2013-08-23 54 views
0

Conditional comments have been deprecated since IE10这破坏了一些遗留代码,用于检测当前的Web浏览器是否为IE浏览器。请参阅https://gist.github.com/padolsey/527683检测Internet Explorer(使用IE10 +也是如此!)

我搜索适用于所有版本Internet Explorer(不仅仅是IE9和更低版本)的解决方案。

感谢

+2

一种解决*什么*什么呢? – Pointy

+0

在这篇文章中的答案可能会指导你在正确的方向:[如何只针对Internet Explorer 10的特定情况,如特定于Internet Explorer的CSS或Internet Explorer特定的JavaScript代码?](http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-circumstances-like-internet-e) –

+0

我想提醒的是,做功能检查通常比较明智和简单浏览器检查。您可能会考虑如何使用MODERNIZR @ http://modernizr.com/ – Steve

回答

-1

继从 “ecstaticpeon” 上https://gist.github.com/padolsey/527683评论,我发现这工作。

自己尝试一下在http://jsfiddle.net/P5z4N/4/

var ie_version = (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; 
}()); 
/*@cc_on 
if (typeof(ie_version) == 'undefined') { 
    ie_version = parseInt(@_jscript_version); 
} 
@*/ 

下面的JavaScript也似乎工作:

尝试在http://jsfiddle.net/e8G3S/1/

// Returns the version of Internet Explorer or a -1 (indicating the use of another browser) 
function getInternetExplorerVersion(){ 
    var rv = -1; // Return value assumes failure. 
    if (navigator.appName == 'Microsoft Internet Explorer'){ 
    var ua = navigator.userAgent; 
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); 
    if (re.exec(ua) != null) 
     rv = parseFloat(RegExp.$1); 
    } 
    return rv; 
} 
function displayAlert(){ 
    var msg = "You're not using Internet Explorer."; 
    var ver = getInternetExplorerVersion(); 
    if (ver > -1){ 
    if (ver <= 8.0){ 
     msg = "You're using Internet Explorer 8 or below" ; 
    } 
    else if (ver >= 9.0 && ver < 10.0){ 
     msg = "You're using IE 9 or above"; 
    } 
    else{ 
     msg = "You're using IE 10 or above"; 
    } 
    } 
    alert(msg); 
+0

这些策略在IE11和未来版本中将失败。 – EricLaw

+0

@EricLaw:你能解释一下为什么吗? –

+0

由于IE团队有意破坏了这些方法,因为它们是互操作性问题的常见来源。具体而言,IE11的AppName不是“Microsoft Internet Explorer”,并且默认的用户代理字符串不再包含“MSIE”。 – EricLaw

相关问题