简体   繁体   English

使用yuicompressor进行条件补偿

[英]Conditional compiliation with yuicompressor

I have the following javascript snippet to detect if a browser is IE 6 - 11: 我有以下javascript代码段来检测浏览器是否为IE 6-11:

var isIE = /*@cc_on!@*/false || !!document.documentMode;

I copied this from an excellent answer on SO here . 我从这里的一个很好的答案中复制了这一点 However when i go to minify the script with yuicompressor, it breaks it with the following errors: 但是,当我使用yuicompressor缩小脚本时,它会因以下错误而中断:

[ERROR] in utils.js
  349:33:missing ; before statement
[ERROR] in utils.js
  1:0:Compilation produced 1 syntax errors.
org.mozilla.javascript.EvaluatorException: Compilation produced 1 syntax errors.
    at com.yahoo.platform.yui.compressor.YUICompressor$1.runtimeError(YUICompressor.java:172)
    at org.mozilla.javascript.Parser.parse(Parser.java:396)
    at org.mozilla.javascript.Parser.parse(Parser.java:340)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:315)
    at com.yahoo.platform.yui.compressor.JavaScriptCompressor.<init>(JavaScriptCompressor.java:536)
    at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:21)

I read up on yuicompressor and found that it can preserve javascript block comments when they are like so: /*! comment */ 阅读了yuicompressor ,发现它可以保留javascript块注释,例如: /*! comment */ /*! comment */ , however changing the snippet to the following does not fix it: /*! comment */ ,但是将代码段更改为以下内容并不能解决该问题:

var isIE = /*!@cc_on!@*/false || !!document.documentMode;

The error messages are still the same. 错误消息仍然相同。 And I also wonder if this would break the conditional compilation? 而且我还想知道这是否会破坏条件编译? All the examples of conditional compilation open a comment like so /*@ and close it like so: @*/ which makes me think that conditional compilation only works with this combination of comment symbols. 条件编译的所有示例都/*@这样的方式打开注释,并以如下方式将其关闭: @*/ ,这使我认为条件编译仅适用于注释符号的这种组合。

Does anybody know how I can get this working? 有人知道我该如何工作吗? Other solutions which maintain the same functionality and pass correctly through yuicompressor are also acceptable as an answer here. 保持相同功能并正确通过yuicompressor的其他解决方案也可以作为答案。

An alternative method to detect IE is to use a global variable for IE <= 9. This will will pass yuicompressor validation: 检测IE的另一种方法是对IE <= 9使用全局变量。这将通过yuicompressor验证:

<html>
<body>
<!--[if IE]><script>isIE9OrLess = true;</script><![endif]-->
<script>
var isIE9OrLess = (typeof isIE9OrLess !== 'undefined');
document.write('are we in ie <= 9? ' + (isIE9OrLess ? 'yes' : 'no') + '<br>');
// detect ie > 9 using document.documentMode
var isIE = isIE9OrLess || !!document.documentMode;
document.write('are we in ie at all? ' + (isIE ? 'yes' : 'no'));
</script>
</body>
</html>

Its not ideal though since it only bypasses the problem without actually solving it. 它不是理想的,因为它仅绕过问题而没有实际解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM