JQuery判断当前设备是否为移动终端、判断当前浏览器内核类型

admin

文章最后更新时间:2024年05月27日

判断移动终端

JavaScript中,可以使用navigator对象的userAgent属性来判断当前设备是否为移动设备,并使用特定的正则表达式匹配不同的浏览器内核。window.navigator.userAgent属性可以返回当前浏览器的用户代理字符串。通过判断用户代理字符串中是否包含移动设备的关键词,我们可以得知用户是在手机端还是在电脑端访问。下面是一个JavaScript示例代码,演示了如何使用window.navigator.userAgent属性来判断手机端:

$(document).ready(function() {
    var isMobileDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    var isSafari = /Safari/.test(navigator.userAgent) && /AppleComputer/.test(navigator.vendor);
    var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
    var isFirefox = /Firefox/.test(navigator.userAgent);
 
    if (isMobileDevice) {
        console.log("移动设备");
        if (isSafari) {
            console.log("Safari 浏览器内核");
        } else if (isChrome) {
            console.log("Chrome 浏览器内核");
        } else if (isFirefox) {
            console.log("Firefox 浏览器内核");
        }
    } else {
        console.log("桌面设备");
    }
});

或者更简洁一些的JQuery方法:

var sj=navigator.userAgent.match(/iPad|iPhone|Linux|Android|iPod/i) != null;
if(sj){
    console.log('移动设备Wap端'); 
}else {
    console.log('PC端'); 
};

当然也可以用JQuery分别判断是安卓终端还是IOS终端设备:

var ios = navigator.userAgent.match(/iPad|iPhone|iPod/i) != null;
var Android = navigator.userAgent.match(/Linux|Android/i) != null;
var IEMobile = navigator.userAgent.match(/IEMobile/i) != null;
if(ios){
    console.log('苹果ios终端'); 
}else if (Android){
    console.log('安卓Android终端'); 
}else if (IEMobile){
    console.log('Winphone手机终端'); 
};

判断浏览器内核

在判断浏览器内核方面也可以比较全面:

function myexplorer(){
    var explorer = window.navigator.userAgent;
    if (explorer.indexOf("QQBrowser")>=0 || explorer.indexOf("QQ")>=0){
	    return myexplorer="腾讯QQ浏览器";
    }else if(explorer.indexOf("Safari")>=0 && explorer.indexOf("MetaSr")>=0){
	    return myexplorer="搜狗浏览器";
    }else if (!!window.ActiveXObject || "ActiveXObject" in window){//IE
        if (!window.XMLHttpRequest){return myexplorer="IE6";
        }else if (window.XMLHttpRequest && !document.documentMode){
	        return myexplorer="IE7";
        }else if (!-[1,] && document.documentMode && !("msDoNotTrack" in window.navigator)){
	        return myexplorer="IE8";
        }else{//IE9 10 11
            var hasStrictMode=(function(){"use strict";return this===undefined;}()); 
            if (hasStrictMode){
                if (!!window.attachEvent){return myexplorer="IE10";}else{return myexplorer="IE11";}
            }else{
                return myexplorer="IE9";
            }
        }
    }else{//非IE
        if (explorer.indexOf("LBBROWSER") >= 0){return myexplorer="猎豹";
        }else if(explorer.indexOf("360ee")>=0){return myexplorer="360极速浏览器";
        }else if(explorer.indexOf("360se")>=0){return myexplorer="360安全浏览器";
        }else if(explorer.indexOf("se")>=0){return myexplorer="搜狗浏览器";
        }else if(explorer.indexOf("aoyou")>=0){return myexplorer="遨游浏览器";
        }else if(explorer.indexOf("qqbrowser")>=0){return myexplorer="QQ浏览器";
        }else if(explorer.indexOf("baidu")>=0){return myexplorer="百度浏览器";
        }else if(explorer.indexOf("Firefox")>=0){return myexplorer="火狐";
        }else if(explorer.indexOf("Maxthon")>=0){return myexplorer="遨游";
        }else if(explorer.indexOf("Chrome")>=0){return myexplorer="谷歌(或360伪装)";
        }else if(explorer.indexOf("Opera")>=0){return myexplorer="欧朋";
        }else if (explorer.indexOf("TheWorld") >= 0){return myexplorer="世界之窗";
        }else if(explorer.indexOf("Safari")>=0){return myexplorer="苹果";
        }else{return myexplorer="其他";
        }
    }
};

苹果cms设备判断

苹果cms内置home.js里的一段浏览设备判断参考:

var MAC={
    'Url': document.URL,
    'Title': document.title,
    'UserAgent' : function(){
        var ua = navigator.userAgent;//navigator.appVersion
        return {
            'mobile': !!ua.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            'ios': !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            'android': ua.indexOf('Android') > -1 || ua.indexOf('Linux') > -1, //android终端或者uc浏览器
            'iPhone': ua.indexOf('iPhone') > -1 || ua.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
            'iPad': ua.indexOf('iPad') > -1, //是否iPad
            'trident': ua.indexOf('Trident') > -1, //IE内核
            'presto': ua.indexOf('Presto') > -1, //opera内核
            'webKit': ua.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            'gecko': ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') == -1, //火狐内核
            'weixin': ua.indexOf('MicroMessenger') > -1 //是否微信 ua.match(/MicroMessenger/i) == "micromessenger",
        };
    }(),
};


文章版权声明:除非注明,否则均为十八码原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,866人围观)

还没有评论,来说两句吧...

目录[+]