0

I have the following javascript snippet to detect if a browser is 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:

[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 */, however changing the snippet to the following does not fix it:

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.

mulllhausen
  • 4,225
  • 7
  • 49
  • 71

1 Answers1

0

An alternative method to detect IE is to use a global variable for IE <= 9. This will will pass yuicompressor validation:

<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.

mulllhausen
  • 4,225
  • 7
  • 49
  • 71