首页 > 如何针对手机浏览器和桌面浏览器分别执行不同的 javascript 语句?

如何针对手机浏览器和桌面浏览器分别执行不同的 javascript 语句?

比如有些语句想在桌面浏览器下执行,但不希望手机浏览器执行,有什么办法?


判断ua

navigator.userAgent返回的有一项是操作系统


navigator.userAgent.toLowerCase().indexOf('mobile') > -1


整理于网络:

//判断是否是手机访问;
var ua = navigator.userAgent;
var ipad = ua.match(/(iPad).*OSs([d_]+)/),
    isIphone = !ipad && ua.match(/(iPhonesOS)s([d_]+)/),
    isAndroid = ua.match(/(Android)s+([d.]+)/),
    isMobile = isIphone || isAndroid,
    isWeiXin = ua.match(/MicroMessenger/);
if(isMobile || isWeiXin) {
}else{
}

var ua = window.navigator.userAgent.toLowerCase();
    if (ua.indexOf('android') != -1) {
        location.replace('android.html');
    } else if (ua.indexOf('iphone') != -1) {
        location.replace('iphone.html');
    }else{
        location.replace('pc.html');
    }


参考: http://.com/q/1010000000227004#a-1020000000267632

【热门文章】
【热门文章】