简体   繁体   English

IE版本,在javascript中没有条件注释

[英]IE version Without conditional comments in javascript

I know how to test an ie version using the conditional comments . 我知道如何使用条件注释来测试ie版本。 But, I found out a good and clean way to do it, by testing a javascript method and check if it's supported or not, like: 但是,通过测试一个javascript方法并检查它是否受支持,我找到了一种很好且干净的方法,例如:

if(  document.addEventListener  ){
    alert("you got IE9 or greater");
}

But, I don't know how to test it if it's an specific version (like, I want to do something if it's IE8 , but not if it's IE7 or IE9 nor other version. 但是,如果它是特定版本,我不知道如何进行测试(例如,如果它是IE8 ,我想做点什么,但是如果它是IE7IE9或其他版本,则不行。

I could use James Panolsey's solution 我可以使用James Panolsey的解决方案

var ie = (function(){

    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');

    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());

But it does test using Conditional Comments. 但是它确实使用条件注释进行测试。


Why not Conditional Comments? 为什么不附加条件注释? Because of the Standards mode . 由于采用Standards mode As Microsoft states in their website and in the dev center , they do NOT support it in their new versions: 正如Microsoft在其网站开发中心中所述 ,他们支持其新版本:

Important As of Internet Explorer 10, conditional comments are no longer supported by standards mode. 重要从Internet Explorer 10开始,标准模式不再支持条件注释。 Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser. 使用功能检测可为浏览器不支持的网站功能提供有效的后备策略。 For more info about standards mode, see Defining Document Compatibility. 有关标准模式的更多信息,请参见定义文档兼容性。


Testing by a valid Method is more efficient in my POV, But I don't know how to do it in a way to specific the version ... ("Using this you can test for that version"). 在我的POV中,通过有效方法进行测试更有效,但是我不知道如何以特定版本进行测试……(“使用可以测试版本”)。 The only one that I know is the above one that I used as example. 我所知道的唯一一个是我作为示例使用的上述一个。 Is there others methods that I could use to test it? 还有其他方法可以测试吗? or not? 或不?

While feature detection can be very useful, and is sometimes the quickest way to distinguish between supportive and non-supportive browser versions, detecting IE and its versions is actually a piece of cake: 虽然功能检测非常有用,并且有时是区分支持浏览器版本和不支持浏览器版本的最快方法,但是检测IE及其版本实际上是小菜一碟:

var uA = navigator.userAgent;
var browser = null;
var ieVersion = null;
var htmlTag = document.documentElement;

if (uA.indexOf('MSIE 6') >= 0) {
    browser = 'IE';
    ieVersion = 6;
}
if (uA.indexOf('MSIE 7') >= 0) {
    browser = 'IE';
    ieVersion = 7;
}
if (document.documentMode) { // as of IE8; strictly IE proprietary 
    browser = 'IE';
    ieVersion = document.documentMode;
}

// Using it can be done like this: 
if (browser == 'IE' && ieVersion == 11)
    htmlTag.className += ' ie11';

.
That's all you need. 这就是您所需要的。 You're catching higher IEs in lower Modes/Views as well with this script, including Compatibility ~. 使用此脚本(包括兼容性〜),还可以在较低的模式/视图中捕获较高的IE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM