Search code examples
javascriptgwtjsniconditional-compilation

Javascript conditional compilation in GWT JSNI


I have this conditional compilation statement which evaluates whether the browser is IE or not:

ie = /*@cc_on!@*/false;
if (ie) {
 //do IE specific stuff..
}

I need to use it in GWT within JSNI. However when I do this:

public native void JS()  /*-{
 ie = /*@cc_on!@*/false;
}-*/;

I am getting an error on that line like Syntax error, } expected. I tried to eval it, still the error persists. How can I fix it?


Solution

  • /*@cc_on!@*/ is not a regular expression, but a multi-line JavaScript comment (/* .. comment .. */). In Internet Explorer, this is more than a comment. The code is parsed and evaluated (this feature is called conditional compilation).

    The */ in your code ends the GWT-specific /*-{ section, causing the error to show up.

    The solution is to use a different way to write down the comment:

    var ie = false;
    //@cc_on ie = true;
    

    Another method, also safe against JavaScript minifiers is:

    var ie = eval('/*@cc_on!@*'+'/false');