Saturday, May 14, 2011

extjs 'events' is null or not an object error in IE (Internet Explorer 7, 8)

I have been working on this extjs application for a while. It works fine in firework, chrome and IE 9. But when i change to IE 7, i got this error message "'events' is null or not an object".  As usual, IE's error message doesn't make much sense, and it shows me the error occurs somewhere in extjs library. I know it couldn't be extjs causing this issue because i've seen much more complicated extjs applications and they work fine in IE 7. It must be my code.

Actually, i can almost guess what causes the problem: It must be some extra commas (And it turned out i was right at this). Check this code:

//work in firefox/chrome/IE9, but not in IE7,8
var me = {
      name : henry,
      gender: m,   //IE7 doesn't allow this last extra comma
}

//work in firefox/chrome/IE9, but not in IE7,8
var language = [
  'php',
 'java',  //IE7 doesn't allow this last extra comma
]

if we remove the last comman in the me object and language array, the code will work in IE7 as well.

I am so used to putting comma at the last item of an array or object because that is what i usually do in PHP array. PHP array also allows you to put extra comma at the last item:

$config = array(
        'db' => 'mysql',
        'username' => 'username', 
);

In the above code, the last comma brings so much convenience. If i need to add more config options, i simply add them at the bottom without worrying breaking anything.

Although I know what the reason is, but the problem is, how to find it? My IDE, Zend Studio almost helps me nothing in finding javascript coding problems. It can only remind you to put semicolon. So i turned out to netbean 7. In supporting javascript coding, netbean 7 is much better than Zend Studio(and Eclipse). At least it can warn you last extra comma in object is not supported by IE. But the funny thing is, it won't warn you that last extra comma in array is not supported by IE either!

No matter what, based on my experience so far, there are two headaches when working on big javascript projects. 1.We don't have a good IDE especially for javascript(At least i haven't found out yet). 2.Debugging. Debugging javascript is quite painful due to those misleading error messages.

3 comments:

debdev said...

Hey, Thanks Helped a big deal this is also an IE 9 issue!

Wahab said...

I am also getting this error "Message: 'events' is null or not an object"

I have looked for all trailing commas and haven't found one.

Do you have any idea why this is happening

Anonymous said...

Wahab, I am also having the same issue. I have found that most people say run your code through http://jslint.com/ to make sure you didn't miss anything.