簡體   English   中英

檢測較舊的IE版本

[英]Detect older IE versions

我需要檢測用戶是否從jQuery插件運行舊版本的IE(IE9很好),所以我無法控制HTML。

我們不鼓勵解析用戶代理字符串並使用$.browser.msie $.support方法也沒有涵蓋這個問題。

所以,我發現這有效,但我不確定它是否是“良好做法”。

$('body').append('<!--[if lte IE 8]><script>$("body").addClass("oldie");</script><![endif]-->');
var old_ie = $('body').is('.oldie');

你會使用這種方法還是堅持解析用戶代理字符串(我需要弄清楚它是否是IE並得到版本號)?

你可以運行它

var ie = (function () {
    var undef, v = 3, div = document.createElement('div');

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

    return v > 4 ? v : undef;
}());

檢測IE的版本。

資料來源: http //ajaxian.com/archives/attack-of-the-ie-conditional-comment

接着

if ( ie < 9 ) {
    // do your stuff, for instance:
    window.location = 'http://getfirefox.com'; // :p
}

您沒有在您的問題中明確提到為什么您需要檢測IE9,因此通常以下建議成立:

您應該檢測特定功能,而不是檢測特定的瀏覽器/版本。 Modernizr是一個開始尋求幫助的好地方。

https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx請參閱上面的鏈接,這可能是正式答案:)簡而言之,在您的html頁面中編寫以下代碼。

<!--[if gte IE 9]>
<p>You're using a recent version of Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]-->

<!--[if lt IE 9]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]-->

<![if !IE]>
<p>You're not using Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]>

這個怎么樣:

if (!($.browser.msie && $.browser.version < 9.0)) {
    // Apply only to IE8 and lower
}

請注意,IE8顯示為IE7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM