Internet Explorer 9对象检测

FUR*_*GIN 1 javascript object-detection capability internet-explorer-9

我正在寻找能识别IE9的物体检测能力检查.你能帮助我吗?

Mat*_*ens 6

看看这个片段詹姆斯Padolsey:

// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
//     ie === undefined
// If you're in IE (>=5) then you can determine which version:
//     ie === 7; // IE7
// Thus, to detect IE:
//     if (ie) {}
// And to detect the version:
//     ie === 6 // IE6
//     ie > 7 // IE8, IE9 ...
//     ie < 9 // Anything less than IE9
// ----------------------------------------------------------

// UPDATE: Now using Live NodeList idea from @jdalton

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;

}());
Run Code Online (Sandbox Code Playgroud)

之后,您可以像这样使用它:

if (ie == 9) {
  // It’s IE9!
  // Insert your code here
}
Run Code Online (Sandbox Code Playgroud)

这里的好处是它不会嗅探UA字符串(这本身就不可靠) - 而是使用条件注释,它在IE中可靠地工作.

这可用于检测IE5-9.

  • @FURKANILGIN:http://stackoverflow.com/questions/8187346/html-conditional-statement-hack-sees-ie-as-wrong-version-when-live (2认同)