如果我们网站的用户使用的是v9之前版本的Internet Explorer,我希望将他们弹出到一个错误页面。不值得我们花时间和金钱去支持iev9之前的版本。所有其他非ie浏览器的用户都没问题,不应该被弹出。以下是提议的代码:
if(navigator.appName.indexOf("Internet Explorer")!=-1){ //yeah, he's using IE
var badBrowser=(
navigator.appVersion.indexOf("MSIE 9")==-1 && //v9 is ok
navigator.appVersion.indexOf("MSIE 1")==-1 //v10, 11, 12, etc. is fine too
);
if(badBrowser){
// navigate to error page
}
}
这段代码能行吗?
为了阻止一些可能会出现在我面前的评论:
Yes, I know that users can forge their useragent string. I'm not concerned.
Yes, I know that programming pros prefer sniffing out feature-support instead of browser-type but I don't feel this approach makes sense in this case. I already know that all (relevant) non-IE browsers support the features that I need and that all pre-v9 IE browsers don't. Checking feature by feature throughout the site would be a waste.
Yes, I know that someone trying to access the site using IE v1 (or >= 20) wouldn't get 'badBrowser' set to true and the warning page wouldn't be displayed properly. That's a risk we're willing to take.
Yes, I know that Microsoft has "conditional comments" that can be used for precise browser version detection. IE no longer supports conditional comments as of IE 10, rendering this approach absolutely useless.
还有其他明显需要注意的问题吗?
下面的代码依赖标识了所有情况下的IE版本(IE<=9, IE10, IE11和IE/Edge)
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
裁判:https://codepen.io/gapcode/pen/vEJNZN
下面的代码依赖标识了所有情况下的IE版本(IE<=9, IE10, IE11和IE/Edge)
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
裁判:https://codepen.io/gapcode/pen/vEJNZN
使用特征检测来检测IE版本(IE6+,在IE6之前的浏览器被检测为6,非IE浏览器返回null):
var ie = (function (){
if (window.ActiveXObject === undefined) return null; //Not IE
if (!window.XMLHttpRequest) return 6;
if (!document.querySelector) return 7;
if (!document.addEventListener) return 8;
if (!window.atob) return 9;
if (!document.__proto__) return 10;
return 11;
})();
编辑:为了方便起见,我创建了一个bower/npm repo: ie-version
更新:
更紧凑的版本可以写成一行:
return window.ActiveXObject === undefined ? null : !window.XMLHttpRequest ? 6 : !document.querySelector ? 7 : !document.addEventListener ? 8 : !window.atob ? 9 : !document.__proto__ ? 10 : 11;
不要把这么简单的事情过分复杂化。只需使用一个简单的JScript条件注释。它是最快的,因为它不需要向非IE浏览器添加代码来进行检测,而且它的兼容性可以追溯到支持HTML条件注释之前的IE版本。简而言之,
var IE_version=(-1/*@cc_on,@_jscript_version@*/);
小心小注释:大多数(如果不是全部)会将特殊条件注释误认为常规注释,并将其删除
基本上,上面的代码将IE_version的值设置为您正在使用的IE版本,或者-1(如果您没有使用IE)。现场演示:
var IE_version = (1 / * @cc_on @_jscript_version@ * /);
如果(IE_version ! = = 1) {
文档。write("<h1>You are using Internet Explorer " + IE_version + "</h1>");
}其他{
文档。write("<h1>您没有使用低于11的Internet Explorer版本</h1>");
}
这是基于条件注释只在旧版本的Internet Explorer中可见这一事实,而IE将@_jscript_version设置为浏览器的版本。例如,如果你使用Internet Explorer 7,那么@_jscript_version将被设置为7,因此,将执行的后处理javascript实际上是这样的:
var IE_version=(-1,7);
结果是7。