在研究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
。
顯然維基百科需要一些幫助,我需要得到這個工作。誰能幫我嗎?
「更強大的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
@_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
@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