Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中检测IE版本(v9之前的版本)(Detect IE version (prior to v9) in JavaScript)

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.(如果他们使用的是v9之前的Internet Explorer版本,我想将我们网站的用户退回至错误页面。)

It's just not worth our time and money to support IE pre-v9 .(支持IE pre-v9只是不值得我们花费时间和金钱。) Users of all other non-IE browsers are fine and shouldn't be bounced.(使用其他所有非IE浏览器的用户都可以使用,不应被退回。) 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:(首先,我将发表一些评论:) Yes, I know that users can forge their useragent string.(是的,我知道用户可以伪造他们的useragent字符串。) 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.(我已经知道所有(相关)非IE浏览器都支持我需要的功能,而所有pre-v9 IE浏览器均不支持。) 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.(是的,我知道尝试使用IE v1 (或> = 20)访问该网站的人不会将'badBrowser'设置为true,并且警告页面将无法正确显示。) 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.(是的,我知道Microsoft具有可用于精确浏览器版本检测的“条件注释”。) IE no longer supports conditional comments as of IE 10 , rendering this approach absolutely useless.(从IE 10 ,IE不再支持条件注释,从而使此方法绝对无用。) Any other obvious issues to be aware of?(还有其他明显的问题要注意吗?)   ask by Chad Decker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is my preferred way of doing it.(这是我的首选方式。)

It gives maximum control.(它提供了最大的控制权。) (Note: Conditional statements are only supported in IE5 - 9.)((注意:仅IE5-9支持条件语句。)) First set up your ie classes correctly(首先正确设置您的ie类) <!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> Then you can just use CSS to make style exceptions, or, if you require, you can add some simple JavaScript:(然后,您可以仅使用CSS来创建样式异常,或者,如果需要,可以添加一些简单的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)); Thanks to Paul Irish .(感谢Paul Irish 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...