IE error: Expected identifier, string or number

I have made this mistake a couple of times and it's one of the most annoying IE errors:
  • it will break all your JavaScript in IE
  • it's a nightmare to debug - even with Microsoft Script Debugger
  • this bug is not present in Safari, Opera, Chrome or Firefox

The error throw will look like this (in IE 7):

IE error

How to reproduce it

If you put an extra comma in a object or list definition the above error will be raised in IE. Example:

a = {
   b: function() { },
   c: function() { },
}

//Or
a = [1,2,3,4,];

I.e. the extra , confuses IE, but not the other browsers. To fix it, simply don't write the extra comma.

Catching these automatically

To catch these I use following function:

import re

obj_close = re.compile(".{0,15}\},\s*\}", re.MULTILINE)
arr_close = re.compile(".{0,15},\s*\][^/]", re.MULTILINE)
def invalidJSseperators(js):
    all_obj_errs = obj_close.search(js)
    all_arr_errs = arr_close.search(js)
    return all_obj_errs, all_arr_errs
Code · JavaScript 15. Jan 2009
1 comment so far

Ah thanks! This saved me so much time trying to track down a stupid comma.

Signed, "But it works in Firefox" Randy

Post a comment
Commenting on this post has expired.
© 2000-2009 amix. Powered by Skeletonz.