zoukankan      html  css  js  c++  java
  • 如何用js来判断浏览器类型(ie,firefox)等等

    现在网络上的浏览器,操作系统就象中国的方言一样,那个叫多啊!这给我们这些开发人员
    带来了巨大的痛苦!虽然可能大家的喜好不同!用的系统也不同!有人喜欢用ie,有人喜欢用
    firefox,还有人喜欢用腾讯tt,而我喜欢用maxthon.虽然名字可能有很多种,但是内核还是只有
    那么的几种!ie内核,netscape内核!怎么样用js来判断各种浏览器的类型呢!
    在不同的浏览器中对js的支持程度,语法要求都不大一样!下面的代码为判断代码

    <script language="JavaScript">
        <!--
    function getOs()
    {
        var OsObject = "";
       if(navigator.userAgent.indexOf("MSIE")>0) {
            return "MSIE";
       }
       if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
            return "Firefox";
       }
       if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
            return "Safari";
       } 
       if(isCamino=navigator.userAgent.indexOf("Camino")>0){
            return "Camino";
       }
       if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
            return "Gecko";
       }
      
    }
     alert("您的浏览器类型为:"+getOs());
        -->
    </script>

    测试一下,你就可以发现了!我用的maxthon,它告诉我的信息却是msie如下图

    所以说maxthon和ie是用的同一个内核!而firefox则不是.

    js检测浏览器版本代码,兼容ie11 :

    <script type="text/javascript">
                    var userAgent = navigator.userAgent, 
                    rMsie = /(msies|trident.*rv:)([w.]+)/, 
                    rFirefox = /(firefox)/([w.]+)/, 
                    rOpera = /(opera).+version/([w.]+)/, 
                    rChrome = /(chrome)/([w.]+)/, 
                    rSafari = /version/([w.]+).*(safari)/;
                    var browser;
                    var version;
                    var ua = userAgent.toLowerCase();
                    function uaMatch(ua) {
                        var match = rMsie.exec(ua);
                        if (match != null) {
                            return { browser : "IE", version : match[2] || "0" };
                        }
                        var match = rFirefox.exec(ua);
                        if (match != null) {
                            return { browser : match[1] || "", version : match[2] || "0" };
                        }
                        var match = rOpera.exec(ua);
                        if (match != null) {
                            return { browser : match[1] || "", version : match[2] || "0" };
                        }
                        var match = rChrome.exec(ua);
                        if (match != null) {
                            return { browser : match[1] || "", version : match[2] || "0" };
                        }
                        var match = rSafari.exec(ua);
                        if (match != null) {
                            return { browser : match[2] || "", version : match[1] || "0" };
                        }
                        if (match != null) {
                            return { browser : "", version : "0" };
                        }
                    }
                    var browserMatch = uaMatch(userAgent.toLowerCase());
                    if (browserMatch.browser) {
                        browser = browserMatch.browser;
                        version = browserMatch.version;
                    }
                    document.write(browser+version);        
                        </script>
  • 相关阅读:
    Android ListView 列表视图
    android handler msg的使用 实现进度条
    Intent 传递数据
    微服务-springcloud
    微服务-dubbo学习
    日志收集系统
    微服务追踪
    链表有环判断,快慢指针两种方法/合并链表/删除重复元素/二分递归和while
    算法练习,链表二分最大n个
    池以及barrier简单
  • 原文地址:https://www.cnblogs.com/coprince/p/3479647.html
Copyright © 2011-2022 走看看