JS判断浏览器类型与操作系统的方法分析
本文实例讲述了JS判断浏览器类型与操作系统的方法。分享给大家供大家参考,具体如下:
navigator.userAgent:userAgent属性是一个只读的字符串,声明了浏览器用于HTTP请求的用户代理头的值。
navigator.platform:platform属性是一个只读的字符串,声明了运行浏览器的操作系统和(或)硬件平台。
判断浏览器类型
IE浏览器
ie=ua.match(/MSIE\s([\d\.]+)/)||ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i),
IE浏览器版本号
/msie8.0/.test(navigator.userAgent.toLowerCase())
微信浏览器
/micromessenger/.test(navigator.userAgent.toLowerCase())
chrome
/chrome/.test(navigator.userAgent.toLowerCase())
firefox
/safari/.test(navigator.userAgent.toLowerCase())
opera
/micromessgenger/.test(navigator.userAgent.toLowerCase())
判断操作系统类型
win操作系统
navigator.platform=="Win32" navigator.platform=="Windows"
mac操作系统
navigator.platform=="Mac68K" navigator.platform=="MacPPC" navigator.platform=="Macintosh" navigator.platform=="MacIntel"
andorid操作系统
if(/Linux/i.test(navigator.userAgent)){
if(/android/i.test(navigator.userAgent.toLowerCase())){
return"android";
}
}
/**
*@description简单的浏览器检查结果。
*
**`webkit`webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`。
**`chrome`chrome浏览器版本号,如果浏览器为chrome,此属性为`undefined`。
**`ie`ie浏览器版本号,如果浏览器为非ie,此属性为`undefined`。**暂不支持ie10+**
**`firefox`firefox浏览器版本号,如果浏览器为非firefox,此属性为`undefined`。
**`safari`safari浏览器版本号,如果浏览器为非safari,此属性为`undefined`。
**`opera`opera浏览器版本号,如果浏览器为非opera,此属性为`undefined`。
*
*@property{Object}[browser]
*/
browser:(function(ua){
varret={},
webkit=ua.match(/WebKit\/([\d.]+)/),
chrome=ua.match(/Chrome\/([\d.]+)/)||
ua.match(/CriOS\/([\d.]+)/),
ie=ua.match(/MSIE\s([\d\.]+)/)||
ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i),
firefox=ua.match(/Firefox\/([\d.]+)/),
safari=ua.match(/Safari\/([\d.]+)/),
opera=ua.match(/OPR\/([\d.]+)/);
webkit&&(ret.webkit=parseFloat(webkit[1]));
chrome&&(ret.chrome=parseFloat(chrome[1]));
ie&&(ret.ie=parseFloat(ie[1]));
firefox&&(ret.firefox=parseFloat(firefox[1]));
safari&&(ret.safari=parseFloat(safari[1]));
opera&&(ret.opera=parseFloat(opera[1]));
returnret;
})(navigator.userAgent),
/**
*@description操作系统检查结果。
*
**`android`如果在android浏览器环境下,此值为对应的android版本号,否则为`undefined`。
**`ios`如果在ios浏览器环境下,此值为对应的ios版本号,否则为`undefined`。
*@property{Object}[os]
*/
os:(function(ua){
varret={},
//osx=!!ua.match(/\(Macintosh\;Intel/),
android=ua.match(/(?:Android);?[\s\/]+([\d.]+)?/),
ios=ua.match(/(?:iPad|iPod|iPhone).*OS\s([\d_]+)/);
//osx&&(ret.osx=true);
android&&(ret.android=parseFloat(android[1]));
ios&&(ret.ios=parseFloat(ios[1].replace(/_/g,'.')));
returnret;
})(navigator.userAgent),
PS:这里再为大家提供几款相关在线工具供大家参考:
在线浏览器信息检测工具:
http://tools.jb51.net/aideddesign/browser_info
常用浏览器(PC,移动)user-agent:
http://tools.jb51.net/table/useragent
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript事件相关操作与技巧大全》、《JavaScript页面元素操作技巧总结》、《JavaScript操作DOM技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。