在 JavaScript 中检测 IE 版本(v9 之前) [英] Detect IE version (prior to v9) in JavaScript
问题描述
如果我们网站的用户使用的是 v9 之前的 Internet Explorer
版本,我想将他们退回到错误页面.支持 IE pre-v9
不值得我们花时间和金钱.所有其他非 IE 浏览器的用户都很好,不应被退回.这是建议的代码:
I want to bounce users of our web site to an error page if they're using a version of Internet Explorer
prior to v9. It's just not worth our time and money to support IE pre-v9
. Users of all other non-IE browsers are fine and shouldn't be bounced. Here's the proposed code:
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
}
}
这段代码能解决问题吗?
Will this code do the trick?
阻止一些可能会出现在我身上的评论:
To head off a few comments that will probably be coming my way:
- 是的,我知道用户可以伪造他们的
useragent
字符串.我不担心. - 是的,我知道编程专业人士更喜欢嗅探功能支持而不是浏览器类型,但我认为这种方法在这种情况下没有意义.我已经知道所有(相关)非 IE 浏览器都支持我需要的功能,而所有
pre-v9 IE
浏览器不支持.在整个网站上逐个检查功能将是一种浪费. - 是的,我知道有人尝试使用
IE v1
(或 >= 20)访问该站点不会将badBrowser"设置为 true,并且警告页面不会正确显示.这是我们愿意承担的风险. - 是的,我知道 Microsoft 有条件注释",可用于精确的浏览器版本检测.从
IE 10
开始,IE 不再支持条件注释,因此这种方法完全没用.
- 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.
还有其他需要注意的明显问题吗?
Any other obvious issues to be aware of?
推荐答案
这是我的首选方式.它提供了最大程度的控制.(注意:条件语句仅在 IE5 - 9 中受支持.)
This is my preferred way of doing it. It gives maximum control. (Note: Conditional statements are only supported in IE5 - 9.)
首先正确设置您的 ie 类
First set up your ie classes correctly
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
然后你可以只使用 CSS 来创建样式异常,或者,如果你需要,你可以添加一些简单的 JavaScript:
Then you can just use CSS to make style exceptions, or, if you require, you can add some simple JavaScript:
(function ($) {
"use strict";
// Detecting IE
var oldIE;
if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) {
oldIE = true;
}
if (oldIE) {
// Here's your JS for IE..
} else {
// ..And here's the full-fat code for everyone else
}
}(jQuery));
感谢 保罗爱尔兰.
这篇关于在 JavaScript 中检测 IE 版本(v9 之前)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!