zoukankan      html  css  js  c++  java
  • 根据特征的浏览器判断

    今天在csdn上看到一个很久以前用的浏览器判断,大多数都通不过了,现在修改了下,我本机上几个浏览器都测试通过
    var Sys = {},
    ua 
    = navigator.userAgent;
    if (window.ActiveXObject)
        Sys.ie 
    = ua.match(/MSIE ([\d.]+)/)[1]
    else if (window.google && window.chrome){
        Sys.chrome 
    = ua.match(/Chrome\/([\d.]+)/)[1]}
    else if (window.Components)
        Sys.firefox 
    = ua.match(/Firefox\/([\d.]+)/)[1]
    else if (window.opera)
        Sys.opera 
    = ua.match(/Opera.([\d.]+)/)[1]
    else if (!navigator.taintEnabled) 
        Sys.safari 
    = ua.match(/Version\/([\d.]+)/)[1];

    //以下进行测试
    if(Sys.ie) document.write('IE: '+Sys.ie);
    if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
    if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
    if(Sys.opera) document.write('Opera: '+Sys.opera);
    if(Sys.safari) document.write('Safari: '+Sys.safari);

    又整理了一个,特性检测:

    var browser=function(){//尽量使用特性去检测
        var ua=navigator.userAgent,
        b 
    = {
            ie : 
    !!window.ActiveXObject,
            
    //!+'\v1'
            //!!window.VBArray
            ie6 : !"1"[0],
            ie67 : 
    /MSIE [67]/.test(ua),
            ie9 : 
    !!document.documentMode && document.documentMode===9,
            chrome : 
    !!(window.google && window.chrome),
            firefox : 
    !!window.Components,
            safari : 
    !!navigator.vendor && !top.chrome,//(!navigator.taintEnabled && !window.chrome),  奇怪的IE6/7报错,
            opera : !!window.opera,
            version : (ua.match(
    /\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
        };    
        
    return b
    }()
    //显示结果
    document.write("<br/><br/>"+navigator.userAgent+"<br/>")
    var c=browser;
    for(var n in c){
        document.write(n
    +" : "+c[n]+"<br/>")
    }
     

    ua检测:

    var bb=function(){
        
    var ua=navigator.userAgent,
        check
    =function(r){return r.test(ua)},
        b 
    = {
            ie : check(
    /MSIE/),
            ie6 : check(
    /MSIE 6/),
            ie67 : check(
    /MSIE [67]/),
            ie9 : check(
    /MSIE 9/),
            chrome : check(
    /Chrome/),
            firefox : check(
    /Firefox/),
            safari : check(
    /Safari/&& !top.chrome,
            opera : check(
    /Opera/),
            version : (ua.match(
    /\s+(?:MSIE|Chrome|Firefox|Version)[ \/]([\d.]+)/) || [0,0])[1]
        };    
        
    return b
    }()

    相关链接:

    javascript浏览器判断

    Browser detect

  • 相关阅读:
    个人学期总结
    管理信息系统 第三部分 作业
    模型分离(选做)
    密码保护
    实现搜索功能
    完成个人中心—导航标签
    个人中心标签页导航
    评论列表显示及排序,个人中心显示
    完成评论功能
    ASP.NET Core开发者指南
  • 原文地址:https://www.cnblogs.com/sohighthesky/p/1755401.html
Copyright © 2011-2022 走看看