使用javascript实现判断当前浏览器

前端之家收集整理的这篇文章主要介绍了使用javascript实现判断当前浏览器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

写了一个判断当前浏览器类型及版本的方法,只在IE 8/11 、谷歌 、360 浏览器(不完全)上测试过

希望大家提出意见

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'; } } } /** * <a href="https://www.jb51.cc/tag/zhuce/" target="_blank" class="keywords">注册</a>判断<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a> */ 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,document);

@H_404_5@

//使用方式

@H_404_5@

以上所述就是本文的全部内容了,希望大家能够喜欢。

原文链接:https://www.f2er.com/js/54795.html

猜你在找的JavaScript相关文章