开发时遇到需要判断国内不同浏览器的问题,故想从网上查找,但最后也没有找到一种通用的解决方法。判断浏览器大体有两种方法:1、根据userAgent判断,但是userAgent可以被用户进行修改,并且随着浏览器的更新,其相应的userAgent也有可能被修改,所以不宜作为判断的依据。2、根据各种浏览器特有的对象的一些属性判断。但对于判断国内的浏览器来说没啥用。
1 根据userAgent判断
var explorer = window.navigator.userAgent.toLowerCase();
if (explorer.indexOf("qqbrowser") >= 0 || explorer.indexOf("qq") >= 0) { //QQ
} else if (explorer.indexOf("safari") >= 0 && explorer.indexOf("metasr") >= 0) { //搜狗
} else if (!!window.ActiveXObject || "ActiveXObject" in window) {//IE
/* if (!window.XMLHttpRequest) {
return myexplorer = "IE6";
} else if (window.XMLHttpRequest && !document.documentMode) {
return myexplorer = "IE7";
} else if (!-[ 1, ] && document.documentMode
&& !("msDoNotTrack" in window.navigator)) {
return myexplorer = "IE8";
} else {//IE9 10 11
var hasStrictMode = (function() {
"use strict";
return this === undefined;
}());
if (hasStrictMode) {
if (!!window.attachEvent) {
return myexplorer = "IE10";
} else {
return myexplorer = "IE11";
}
} else {
return myexplorer = "IE9";
}
} */
} else {//非IE
if (explorer.indexOf("lbbrowser") >= 0) { //猎豹
} else if(explorer.indexOf("ubrowser") >= 0 || explorer.indexOf("ucbrowser") >= 0){ //uc
} else if (explorer.indexOf("opera") >= 0 || explorer.indexOf("opr") >= 0) { //欧朋
} else if (explorer.indexOf("firefox") >= 0) { //火狐
return true;
} else if (explorer.indexOf("maxthon") >= 0) { //遨游
} else if (explorer.indexOf("chrome") >= 0) { //谷歌(或360伪装)
return true;
}else if (explorer.indexOf("theworld") >= 0) { //世界之窗
} else if (explorer.indexOf("safari") >= 0) { //苹果
} else { //其他
}
}
2 相关判断浏览器的链接
http://blog.sina.com.cn/s/blog_64589b110101bzvh.html
http://www.cnblogs.com/dreamhome/archive/2013/01/27/2878532.html
http://www.cnblogs.com/longze/archive/2013/06/07/3124354.html