我有以下脚本检查我正在运行的IE版本,并使用样式表以及显示警告声明:
var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 9]><i></i><![endif]-->";
var isIeLessThan9 = (div.getElementsByTagName("i").length == 1);
if (isIeLessThan9) {
alert("<=9");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_ie.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_ie.css" type="text/css" charset="utf-8" />');
}
if (!isIeLessThan9) {
alert(">9");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle.css" type="text/css" charset="utf-8" />');
}
正在发生的事情是IE 10及更高版本显示>=10
,任何低于IE 10的版本仍会显示>=10
我该如何解决?
修改 :
这是应该解决问题的更新代码:
<script type="text/javascript">
var isMobile = {
Android: function () {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function () {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i);
},
any: function () {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
navigator.sayswho= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
return M.join(' ');
})();
var browserversion1 = navigator.sayswho.split(" ");
var browserversion2 = browserversion1[1].split(".")[0].split(",");
var isIeLessThan10 = browserversion1[0] == "IE" && browserversion2[0] < 10;
if (!isMobile.any()) {
if (isIeLessThan10) {
alert("IE<10");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_ie.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_ie.css" type="text/css" charset="utf-8" />');
}
else {
alert("IE>=10 || !IE");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle.css" type="text/css" charset="utf-8" />');
}
}
else {
alert("mobile");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_mobile.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_mobile.css" type="text/css" charset="utf-8" />');
}
</script>
IE8和FF都显示警告IE>=10 || !IE
答案 0 :(得分:2)
我宁愿使用此功能来获取浏览器及其版本,而不是依赖于html标记计数,这可以随着您的内容轻松更改:JSFiddle
navigator.sayswho= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
return M.join(' ');
})();
var browserversion1 = navigator.sayswho.split(" ");
var browserversion2 = browserversion1[1].split(".")[0].split(",");
var isIeLessThan10 = (browserversion1[0] == "IE" || browserversion1[0] == "MSIE") && browserversion2[0] < 10;