使用javascript实现判断当前浏览器
更新时间:2015年04月14日 11:35:03 投稿:hebedich
这篇文章主要介绍了使用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());
以上所述就是本文的全部内容了,希望大家能够喜欢。
相关文章
JavaScript实现将数组中所有元素连接成一个字符串的方法
这篇文章主要介绍了JavaScript实现将数组中所有元素连接成一个字符串的方法,涉及javascript中采用join方法进行数组转化的技巧,非常具有实用价值,需要的朋友可以参考下2015-04-04
仅IE支持clearAttributes/mergeAttributes方法使用介绍
仅IE中HTMLElement元素具有clearAttributes/mergeAttributes方法,它们都是非标准的2012-05-05
详解webpack中的hash、chunkhash、contenthash区别
这篇文章主要介绍了详解webpack中的hash、chunkhash、contenthash区别,详细的介绍了hash、chunkhash、contenthash的用法和区别,有兴趣的可以了解一下2018-01-01
手写的一个兼容各种浏览器的javascript getStyle函数(获取元素的样式)
这篇文章主要介绍了手写的一个兼容各种浏览器的javascript getStyle函数,用来取元素的样式,需要的朋友可以参考下2014-06-06


最新评论