js浏览器检测的一些方法

简单检测办法

ie678 = !+"\v1" ;
ie678 = !-[1,];//IE9预览版中失效
ie678 ='\v'=='v' ;
ie678 = ('a~b'.split(/(~)/))[1] == "b"
ie678 = 0.9.toFixed(0) == "0"
IE8 = window.toStaticHTML
IE9 = window.msPerformance 
IE6-10 = !!document.createStyleSheet
ie = !!document.recalc
ie = !!window.VBArray
ie = !!window.ActiveXObject
ie678 = 0//@cc_on+1;
ie = !!window.createPopup;
ie = /*@cc_on!@*/!1;
ie = document.expando;//document.all在opera firefox的古老版本也存在
ie = /\w/.test('\u0130') //由群里的abcd友情提供
ie6 = !"1"[0] //利用IE6或IE5的字符串不能使用数组下标的特征
ie8 = !!window.XDomainRequest;
ie9 =  document.documentMode && document.documentMode === 9;

自创,基于条件编译的嗅探脚本,IE会返回其JS引擎的版本号,非IE返回0

var ieVersion = eval("''+/*@cc_on"+" @_jscript_version@*/-0")*1
ie9 = ieVersion === 5.9
ie8 = ieVersion === 5.8
ie7 = ieVersion === 5.7
ie6 = ieVersion === 5.6
ie5 = ieVersion === 5.5

其它

isIE11 =navigator.userAgent.indexOf("re:11")>0
netscape = !!window.GeckoActiveXObject 
gecko  = !!window.netscape //包括firefox
firefox = !!window.Components
firefox = !!window.updateCommands
firefox = !!window.sidebar
safari = !!(navigator.vendor && navigator.vendor.match(/Apple/))
safari = window.openDatabase && !window.chrome;
chrome= !!(window.chrome && window.google)
opera=!!window.opera ;
wpIE = 'msmaxtouchpoints' in window.navigator //判定当前是否在IE的移动版中

傲游2 3

maxthon = /maxthon/i.test(navigator.userAgent)

360安全浏览器

is360se = /360se/i.test(navigator.userAgent)

判定IE版本

//2010.10.1
ie = (function(undefined){
    var v = 3, div = document.createElement('div');
    while (
    div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
    div.getElementsByTagName('i')[0]);
    return v> 4 ? v : undefined;
}());
//2011.2.24
ie = (function() {
    var v = 3, div = document.createElement('div');
    while (div.innerHTML = '<!--[if gt IE '+(++v)+']>1<![endif]-->', div.innerHTML);
    return v > 4 ? v : !v;
 }());

手机的相关判定

//2011.9.21
isIPhone = /iPhone/i.test(navigator.userAgent);
isIPhone4 = window.devicePixelRatio >= 2

在网页中,pixel与point比值称为device-pixel-ratio,普通设备都是1,iPhone 4是2,有些Android机型是1.5

 var ua = navigator.userAgent;
 isAndroid = /Android/i.test(ua);
 isBlackBerry = /BlackBerry/i.test(ua)
 isWindowPhone = /IEMobile/i.test(ua)
 isIOS = /iPhone|iPad|iPod/i.test(ua)
 isMobile = isAndroid || isBlackBerry || isWindowPhone || isIOS

 if (window.external + "" == "undefined" || window.external == undefined) {
    //网站不能识别你的浏览器 不支持window.external 很可能为360浏览器
    browseInfo.extend = "360SE";
    browseInfo.name = "360浏览器";
 }
 var pf = (navigator.platform || "").toLowerCase(),
    ua = navigator.userAgent.toLowerCase(),
    s;

 function toFixedVersion(ver, floatLength) {
    ver = ("" + ver).replace(/_/g, ".");
    floatLength = floatLength || 1;
    ver = String(ver).split(".");
    ver = ver[0] + "." + (ver[1] || "0");
    ver = Number(ver).toFixed(floatLength);
    return ver;
 }

 function updateProperty(target, name, ver) {
    target = QApp[target]
    target.name = name;
    target.version = ver;
    target[name] = ver;
 }
 // 提供三个对象,每个对象都有name, version(version必然为字符串)
 // 取得用户操作系统名字与版本号,如果是0表示不是此操作系统
 var platform = QApp.platform = {
    name: (window.orientation != undefined) ? 'iPod' : (pf.match(/mac|win|linux/i) || ['unknown'])[0],
    version: 0,
    iPod: 0,
    iPad: 0,
    iPhone: 0,
    android: 0,
    win: 0,
    linux: 0,
    mac: 0
 };

 (s = ua.match(/windows ([\d.]+)/)) ? updateProperty("platform", "win", toFixedVersion(s[1])) : (s = ua.match(/windows nt ([\d.]+)/)) ? updateProperty("platform", "win", toFixedVersion(s[1])) : (s = ua.match(/linux ([\d.]+)/)) ? updateProperty("platform", "linux", toFixedVersion(s[1])) : (s = ua.match(/mac ([\d.]+)/)) ? updateProperty("platform", "mac", toFixedVersion(s[1])) : (s = ua.match(/ipod ([\d.]+)/)) ? updateProperty("platform", "iPod", toFixedVersion(s[1])) : (s = ua.match(/ipad[\D]*os ([\d_]+)/)) ? updateProperty("platform", "iPad", toFixedVersion(s[1])) : (s = ua.match(/iphone ([\d.]+)/)) ? updateProperty("platform", "iPhone", toFixedVersion(s[1])) : (s = ua.match(/android ([\d.]+)/)) ? updateProperty("platform", "android", toFixedVersion(s[1])) : 0;
 //============================================
 //取得用户的浏览器名与版本,如果是0表示不是此浏览器
 var browser = QApp.browser = {
    name: "unknown",
    version: 0,
    ie: 0,
    firefox: 0,
    chrome: 0,
    opera: 0,
    safari: 0,
    mobileSafari: 0,
    adobeAir: 0 //adobe 的air内嵌浏览器
 };

 (s = ua.match(/trident.*; rv:([\d.]+)/)) ? updateProperty("browser", "ie", toFixedVersion(s[1])) : //IE11的UA改变了没有MSIE
 (s = ua.match(/msie ([\d.]+)/)) ? updateProperty("browser", "ie", toFixedVersion(s[1])) : (s = ua.match(/firefox\/([\d.]+)/)) ? updateProperty("browser", "firefox", toFixedVersion(s[1])) : (s = ua.match(/chrome\/([\d.]+)/)) ? updateProperty("browser", "chrome", toFixedVersion(s[1])) : (s = ua.match(/opera.([\d.]+)/)) ? updateProperty("browser", "opera", toFixedVersion(s[1])) : (s = ua.match(/adobeair\/([\d.]+)/)) ? updateProperty("browser", "adobeAir", toFixedVersion(s[1])) : (s = ua.match(/version\/([\d.]+).*safari/)) ? updateProperty("browser", "safari", toFixedVersion(s[1])) : 0;

 //下面是各种微调
 //mobile safari 判断,可与safari字段并存
 (s = ua.match(/version\/([\d.]+).*mobile.*safari/)) ? updateProperty("browser", "mobileSafari", toFixedVersion(s[1])) : 0;

 if (platform.iPad) {
    updateProperty("browser", 'mobileSafari', '0.0');
 }

 if (browser.ie) {
    if (!document.documentMode) {
        document.documentMode = Math.floor(browser.ie)
        //http://msdn.microsoft.com/zh-cn/library/cc817574.aspx
        //IE下可以通过设置 <meta http-equiv="X-UA-Compatible" content="IE=8">改变渲染模式
        //一切以实际渲染效果为准
    } else if (document.documentMode !== Math.floor(browser.ie)) {
        updateProperty("browser", "ie", toFixedVersion(document.documentMode))
    }
 }
 //============================================
 //取得用户浏览器的渲染引擎名与版本,如果是0表示不是此浏览器
 QApp.engine = {
    name: 'unknown',
    version: 0,
    trident: 0,
    gecko: 0,
    webkit: 0,
    presto: 0
 };

 (s = ua.match(/trident\/([\d.]+)/)) ? updateProperty("engine", "trident", toFixedVersion(s[1])) : (s = ua.match(/gecko\/([\d.]+)/)) ? updateProperty("engine", "gecko", toFixedVersion(s[1])) : (s = ua.match(/applewebkit\/([\d.]+)/)) ? updateProperty("engine", "webkit", toFixedVersion(s[1])) : (s = ua.match(/presto\/([\d.]+)/)) ? updateProperty("engine", "presto", toFixedVersion(s[1])) : 0;

 if (QApp.browser.ie) {
    if (QApp.browser.ie == 6) {
        updateProperty("engine", "trident", toFixedVersion("4"));
    } else if (browser.ie == 7 || browser.ie == 8) {
        updateProperty("engine", "trident", toFixedVersion("5"));
    }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值