zoukankan      html  css  js  c++  java
  • [js] js判断浏览器(转)

        (function($, window, document,undefined){
        if(!window.browser){
             
            var userAgent = navigator.userAgent.toLowerCase(),uaMatch;
            window.browser = {}
             
            /**
             * 判断是否为ie
             */
            function isIE(){
                return ("ActiveXObject" in window);
            }
            /**
             * 判断是否为谷歌浏览器
             */
            if(!uaMatch){
                uaMatch = userAgent.match(/chrome/([d.]+)/);
                if(uaMatch!=null){
                    window.browser['name'] = 'chrome';
                    window.browser['version'] = uaMatch[1];
                }
            }
            /**
             * 判断是否为火狐浏览器
             */
            if(!uaMatch){
                uaMatch = userAgent.match(/firefox/([d.]+)/);
                if(uaMatch!=null){
                    window.browser['name'] = 'firefox';
                    window.browser['version'] = uaMatch[1];
                }
            }
            /**
             * 判断是否为opera浏览器
             */
            if(!uaMatch){
                uaMatch = userAgent.match(/opera.([d.]+)/);
                if(uaMatch!=null){
                    window.browser['name'] = 'opera';
                    window.browser['version'] = uaMatch[1];
                }
            }
            /**
             * 判断是否为Safari浏览器
             */
            if(!uaMatch){
                uaMatch = userAgent.match(/safari/([d.]+)/);
                if(uaMatch!=null){
                    window.browser['name'] = 'safari';
                    window.browser['version'] = uaMatch[1];
                }
            }
            /**
             * 最后判断是否为IE
             */
            if(!uaMatch){
                if(userAgent.match(/msie ([d.]+)/)!=null){
                    uaMatch = userAgent.match(/msie ([d.]+)/);
                    window.browser['name'] = 'ie';
                    window.browser['version'] = uaMatch[1];
                }else{
                    /**
                     * IE10
                     */
                    if(isIE() && !!document.attachEvent && (function(){"use strict";return !this;}())){
                        window.browser['name'] = 'ie';
                        window.browser['version'] = '10';
                    }
                    /**
                     * IE11
                     */
                    if(isIE() && !document.attachEvent){
                        window.browser['name'] = 'ie';
                        window.browser['version'] = '11';
                    }
                }
            }
     
            /**
             * 注册判断方法
             */
            if(!$.isIE){
                $.extend({
                    isIE:function(){
                        return (window.browser.name == 'ie');
                    }
                });
            }
            if(!$.isChrome){
                $.extend({
                    isChrome:function(){
                        return (window.browser.name == 'chrome');
                    }
                });
            }
            if(!$.isFirefox){
                $.extend({
                    isFirefox:function(){
                        return (window.browser.name == 'firefox');
                    }
                });
            }
            if(!$.isOpera){
                $.extend({
                    isOpera:function(){
                        return (window.browser.name == 'opera');
                    }
                });
            }
            if(!$.isSafari){
                $.extend({
                    isSafari:function(){
                        return (window.browser.name == 'safari');
                    }
                });
            }
        }
    })(jQuery, window, document);
    alert(window.browser.name);
    console.log(window.browser);
    console.log($.isIE());
    console.log($.isChrome());
    console.log($.isFirefox());
  • 相关阅读:
    理解OO 思想 架构好一个程序之基石!~
    WP7 ZIP 压缩与解压缩
    Gis LBS 应用 剧本 (自己乱想的)
    PHP 入门 环境搭建
    Android 入门必须知道的 英文缩写
    Android 开发 数据结构理解 队列和栈 分析及实现
    c函数中形参为引用的情况;C++中*a和*&a的区别
    如何在.NET中使用尾递归
    [ProjectEuler.net] 25 找到第一个fib数,数位为1000位
    NFA_DFA
  • 原文地址:https://www.cnblogs.com/newpanderking/p/4421531.html
Copyright © 2011-2022 走看看