5

在研究IE的JavaScript條件註釋時,我偶然發現了@cc_on。這似乎工作。然而,在有條件的意見wikipedia entry提供了更強大的IE檢測下面的代碼,特別是IE6:/* @ cc_on和IE6檢測

/*@cc_on 
    @if (@_jscript_version > 5.7) 
    document.write("You are using IE8+"); 

    @elif (@_jscript_version == 5.7 && window.XMLHttpRequest) 
    document.write("You are using IE7"); 

    @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest)) 
    document.write("You are using IE6"); 

    @elif (@_jscript_version == 5.5) 
    document.write("You are using IE5.5"); 

    @else 
    document.write("You are using IE5 or older"); 

@end 

@*/ 

的問題是,我得到一個「預期不變」 JavaScript錯誤的!window.XMLHttpRequest

顯然維基百科需要一些幫助,我需要得到這個工作。誰能幫我嗎?

+0

「更強大的IE檢測」?我認爲有條件的評論提供了最可靠的IE檢測形式。 **不要**依賴JScript版本來確定IE版本,因爲它們是獨立的。請參閱http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html#c1774947112904387635的第4點此外,您完全不應該使用瀏覽器檢測;使用特徵檢測:http://www.nczonline.net/blog/2009/12/29/feature-detection-is-not-browser-detection/ – 2010-03-01 03:43:12

+1

@_jscript_version檢測應僅在您知道jscript引擎的版本,你想以某種方式來區別這種差異 - 例如,模擬一個缺失的功能。例如,JScript的5.1沒有函數的原型呼叫/申請中定義的方法,無論是它有Array.prototype.push。因此,檢測此@_jscript_version併爲這些功能添加編程支持是安全的。然而,正如馬塞爾說,這不是安全的假設的5.1地圖,@_jscript_version到IE5.01,你可以很容易地得到的JScript從Windows Update更新。 – 2010-03-18 08:20:13

+1

@Vitaly:我不想挑剔太多,但是你給的例子可以用功能檢測來測試,所以你也不必測試Javascript版本大於等於1.3的東西。只需使用if(!Function.prototype.call){Function.prototype.call = foo; }'等,就像Oz.js一樣:http://code.google.com/p/oz-js/source/browse/trunk/oz.js#224 – 2010-03-18 19:08:41

回答

4

肯定沒JS專家,但某些搜索發現這個使用jscript_version == 5.7從IE7隔離IE6:

/*@cc_on 
if (@_jscript_version==5.6 || 
    (@_jscript_version==5.7 && 
     navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1)) { 
    //ie6 code 
} 
@*/ 

也許它會指出你在正確的方向。

來源: http://sharovatov.wordpress.com/2009/06/03/efficient-ie-version-targeting/

+0

我想澄清一點,但你說得很對。這個問題結果是試圖在預編譯的條件註釋操作符「@elif」中調用普通的JavaScript。當你只是預編譯jscript版本,並且像傳統JavaScript一樣運行其他東西時,事情編譯和運行都很好。 – Jack 2009-12-04 19:50:48

+0

@Marcel Korpel - 你讀過JBickford所指的博客文章嗎? userAgent檢查用於*條件編譯塊內(僅適用於IE),並且沒有其他方法可以確定在具有jscript 5.7的IE7和具有jscript 5.7的Windows XP SP3上的IE6之間的區別。 – 2010-03-16 06:10:47

+0

@Vitaly:我不得不承認我沒有閱讀過你的文章,而且我對我原來的評論有些苛刻,對此我表示歉意(並且我刪除了評論)。您有一點可以在條件編譯模塊中安全地檢測IE版本(並且這樣做足夠了,還可以檢查JScript版本是否不必要),並且需要使用瀏覽器檢測來檢查':hover'的可能性(據我所知,它們不能被檢測到),但我認爲使用條件註釋是一種更簡潔的方法。也就是說,在大多數情況下,你可以並應該只使用特徵檢測。 – 2010-03-17 22:43:13

3

我找到了解決辦法。代碼如下。

<script type="text/javascript" charset="utf-8"> 
/*@cc_on 
if (@_jscript_version > 5.7) 
document.write("You are using IE8"); 
else if (@_jscript_version == 5.7 && window.XMLHttpRequest) 
document.write("You are using IE7"); 
else if (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest)) 
document.write("You are using IE6"); 
else if (@_jscript_version == 5.5) 
document.write("You are using IE5.5"); 
else 
document.write("You are using IE5 or older"); 
@*/ 
</script> 
+1

你是否失明?這就是他在他的問題中包含的同樣的代碼塊。 – 2010-01-28 16:42:54

+7

嗨喬希,我改變@if ... @ elif ... @其他... @結束如果...其他如果...其他。我在@if中發現了一些代碼... @ elif ... @ else ... @end將無法正確執行,例如** window.XMLHttpRequest **部分。但它在正常的if語句中運行良好。 – 2010-01-29 01:52:32

2

我一直在使用這個漂亮的一個班輪年:

var IE; //@cc_on IE = parseFloat((/MSIE[\s]*([\d\.]+)/).exec(navigator.appVersion)[1]); 

小和精確的(在IE 6-10測試)。

注意那些使用咕嚕聲的人。一定要設置preserveComments:「有些」,如果你使用uglify插件,以確保條件註釋不會被刪除。

+0

你從哪裏找到這個漂亮的單線? – yckart 2013-06-18 23:35:31

+0

@yckart我寫的。你可以在https://github.com/willfarrell/angular-io/blob/master/src/scripts/ie.js看到我的最新版本,其中包括一個chromeframe檢查(這將是不必要的2014年1月,由於鉻名譽沒有更長時間保持)。 – 2013-06-26 21:09:27

0

也許有點遲到了,但我也碰到這個問題,它有一個爆炸,我的解決方案如下,希望它可以幫助https://github.com/davesmiths/isIE

var isIE = false; 
/*@cc_on isIE = @_jscript_version;@*/ 
if (isIE !== false) { 
    if (isIE == 5.8) 
     isIE = 8; 
    else if (isIE == 5.7 && window.XMLHttpRequest) 
     isIE = 7; 
    else if (isIE == 5.7 || isIE == 5.6) 
     isIE = 6; 
    else if (isIE <= 5.5) 
     isIE = 5; 
}