Internet Explorer, Closure Compiler and Trailing Commas
C

2

5

I'm using html5boilerplate build script and when minifying the scripts (which uses Google Closure Compiler)

I'm getting this error

-js.all.minify:
     [echo] Minifying scripts
     [copy] Copying 3 files to /Users/Username/Desktop/Web/intermediate/js
    [apply] /Users/Juan/Desktop/Web/js/plugins.js:117: ERROR - Parse error. Internet Explorer has a non-standard intepretation of trailing commas. Arrays will have the wrong length and objects will not parse at all.
    [apply]                 }, { duration: 727 })
    [apply] 

             ^

But the code DOES work in IE 8 if run uncompiled.

This is the code

anim1.animate({
                    'left': '+=32px',
                    'filter': 'alpha(opacity=100)',
                    '-moz-opacity': '1',
                    '-khtml-opacity': '1',
                    'opacity': '1',
                }, { duration: 727 })

How can I make this code pass Compulsure Compiler?

Thanks

Crutchfield answered 6/8, 2012 at 5:58 Comment(0)
A
10

Remove the superfluous last comma from your object literal:

anim1.animate({
    'left': '+=32px',
    'filter': 'alpha(opacity=100)',
    '-moz-opacity': '1',
    '-khtml-opacity': '1',
    'opacity': '1'      // <-- No comma here.
}, { duration: 727 });  // <-- I'd also suggest a semicolon there.

As the Closure compiler says, literals with such trailing commas cannot be parsed by some browsers.

Aphotic answered 6/8, 2012 at 6:3 Comment(3)
I spend so much on this code I could not see it right in forn of my eyes. I still find it weird that the code works fine in IE 7,8,9 but the compiler treats it some kind of fatal error. Thanks Frédéric.Crutchfield
Here's an article that explains why: enterprisedojo.com/2010/12/19/…. You can convert this error to a warning by using the --jscomp_warning internetExplorerChecks flag.Six
It would have been really nice to have JSHint and JSLint tell me this, as I have just wasted all day on this... SMDH ThanksEladiaelaeoptene
S
4

Or enable EcmaScript 5 mode. Ecmascript 5 does standardize the trailing comma behavior but IE8 does not fully support it ES5 (neither does IE9 which is missing strict mode).

Spinule answered 6/8, 2012 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.