javascript判断移动端还是PC端输出不同代码该怎么搞? 20
functionbrowserRedirect(){varsUserAgent=navigator.userAgent.toLowerCase();varbIsIpad=...
function browserRedirect()
{
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (! (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
document.write("移动端代码");
}
else
{
document.write("PC端代码");
}
}
browserRedirect();
这么写是否正确?有什么更好的方法么? 展开
{
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (! (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
document.write("移动端代码");
}
else
{
document.write("PC端代码");
}
}
browserRedirect();
这么写是否正确?有什么更好的方法么? 展开
展开全部
还可以写得更简单:
function goPAGE() {
if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
window.location.href="你的手机版地址";
}
else {
window.location.href="你的电脑版地址"; }
}
goPAGE();//调用判断函数
更好的办法是:因为UA是可以造假的,不一定100%准,再另外写段代码判断下屏幕宽度什么的更靠谱。
function goPAGE() {
if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
window.location.href="你的手机版地址";
}
else {
window.location.href="你的电脑版地址"; }
}
goPAGE();//调用判断函数
更好的办法是:因为UA是可以造假的,不一定100%准,再另外写段代码判断下屏幕宽度什么的更靠谱。
追问
屏幕宽度判断貌似不行,因为是国外的联盟ad代码,pc是pc的,移动是移动的,pc端的代码无法在移动端展现
追答
判断屏幕宽度跟里面广告代码没半毛关系的,屏幕宽度是指访问者的设备屏宽。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询