5

在研究 IE 的 JavaScript 条件注释时,我偶然发现了@cc_on。这似乎有效。但是,关于条件注释的维基百科条目提供了以下代码,用于更强大的 IE 检测,特别是 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

@*/

问题是,我在!window.XMLHttpRequest.

显然维基百科需要一些帮助,我需要让这个工作。谁能帮我吗?

4

4 回答 4

4

绝对不是 JS 专家,但一些搜索发现这是使用 jscript_version == 5.7 将 IE6 与 IE7 隔离开来的:

/*@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/

于 2009-12-04T16:25:29.417 回答
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>
于 2010-01-28T16:41:06.290 回答
2

多年来我一直在使用这个漂亮的单线:

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

小而准确(在 IE 6-10 中测试)。

请注意那些使用 grunt 的人。uglify如果您使用插件确保不删除条件注释,请务必设置 preserveComments: 'some' 。

于 2013-04-02T16:12:52.417 回答
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;
} 
于 2013-12-13T16:57:20.957 回答