网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 使用javascript实现判断当前浏览器
范文
    写了一个判断当前浏览器类型及版本的方法,只在IE 8/11 、谷歌 、360 浏览器(不完全)上测试过
    希望大家提出意见
    ;(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);
    //使用方式
    console.log(window.browser);
    console.log($.isIE());
    console.log($.isChrome());
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/23 6:08:34