/*@cc_on和IE6检测

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上收到了一个“期望常数”的javascript错误。
显然维基百科需要一些帮助,而我需要让它正常工作。有人能帮帮我吗?

更强大的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/。 - Marcel Korpel
1
只有在您确切了解 jscript 引擎版本之间的差异并希望以某种方式针对这种差异时,才应使用 @_jscript_version 检测。例如,jscript 5.1 没有定义 Function 的 prototype call/apply 方法,也没有 Array.prototype.push。因此,检测此 @_jscript_version 并为这些特性添加编程支持是安全的。但是,正如 Marcel 所说,假设 @_jscript_version 的 5.1 映射到 IE5.01 是不安全的,因为您可能会轻松地从 Windows Update 获取 jscript 更新。 - Vitaly Sharovatov
1
@Vitaly:我不想太挑剔,但你给出的示例可以使用特性检测进行测试,因此您不必再测试JavaScript版本> = 1.3之类的内容。只需像Oz.js一样使用if(!Function.prototype.call){Function.prototype.call = foo;}等即可:http://code.google.com/p/oz-js/source/browse/trunk/oz.js#224 - Marcel Korpel
我意识到这已经过时了,但当我需要进行一些旧浏览器检测时,我偶然发现了这个。仅供参考,在上面的调用中,您应该使用typeof运算符来避免出现错误。这不是我专门测试IE6的方法,但正如我所说,仅供参考,您的代码应该是..if (typeof window.XMLHttpRequest=="undefined") { /* code here */ } - Ric
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/

我想澄清一下,但你基本上是对的。问题在于尝试在预编译条件注释操作符“@elif”等中调用普通JavaScript。当您仅预编译jscript版本,并将其他所有内容作为传统JavaScript运行时,事情就可以编译和运行得很好。 - Jack
@Marcel Korpel - 你读过JBickford所提到的我博客文章了吗?userAgent检查被用于条件编译块(因此仅适用于IE),没有其他方法可以区分具有jscript 5.7的IE7和也具有jscript 5.7的Windows XP SP3上的IE6。 - Vitaly Sharovatov
@Vitaly:我必须承认,我没有阅读你的文章,并且在我的原始评论中有点苛刻,对此我深表歉意(并删除了评论)。你说得对,可以在条件编译块中安全地检测IE版本(仅此就足够了,检查JScript版本是不必要的),并且浏览器检测确实需要检查:hover可能性(据我所知,它们无法被检测到),但我认为使用条件注释是更整洁的方法。话虽如此,在大多数情况下,您可以和应该只使用特征检测。 - Marcel Korpel
@Marcel 仅在条件编译块中检查navigator.userAgent字符串显然是不够的,因为IE6可以同时拥有jscript版本5.6和5.7(安装XPSP3时)。因此,如果@_jscript_version返回5.7,则必须结合两个检查-对于@_jscript_version和navigator.userAgent。 而无法通过条件注释区分具有jscript 5.6或5.7的IE6-[if IE 6]将被两者解析。特征检测在大多数情况下都可以做到,但在某些最模糊的错误情况下,您只需要知道jscript和Trident的确切版本。 - Vitaly Sharovatov
@Vitaly:我同意你的观点,如果你需要特定功能或想解决涉及JScript的错误,那么你需要寻找JScript版本。然而,最初的问题是关于检测IE 6的。 - Marcel Korpel

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
你瞎了吗?那是他在问题中包含的相同代码块。 - Josh Stodola
8
嗨Josh,我将@if...@elif...@else...@end改为if...else if...else。我发现一些@if...@elif...@else...@end中的代码无法正常执行,例如window.XMLHttpRequest部分。但在普通的if语句中它能够很好地工作。 - Just a learner

2

多年来,我一直在使用这个简洁的代码:

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

小而精准(已在IE 6-10中进行了测试)。

对于使用grunt的人,请确保设置preserveComments: 'some',如果您使用uglify插件,则要确保条件注释不被删除。


你在哪里找到这个漂亮的一行代码的? - yckart
@yckart 我写了这个。你可以在 https://github.com/willfarrell/angular-io/blob/master/src/scripts/ie.js 上看到我的最新版本,其中包括一个chromeframe检查(从2014年1月开始将不再需要,因为chrome fame不再维护)。 - will Farrell

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;
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接