views:

50

answers:

3

on what basis does a check if (document.addEventListener) return false and is there a way we can change this ?

+1  A: 

It returns a false value (although not actually false) if it isn't defined.

This is a standard feature (AKA object) detection test.

You could change it by implementing your own version. More usually you'll use this in a wrapper function with the } else { having IE specific handling.

David Dorward
+2  A: 

if (document.addEventListener) evaluates to false if there's no addEventListener method in document. This check is usually done to see if you can use this method to attach event to DOM element (works in most browsers except IE).

is there a way we can change this ?
This question I don't completely understand. Probably, you want something like document.attachEvent('onload', callback); for IE. You can't really add addEventListener method to document (well, maybe you can, but it wouldn't make sense).

Docs for addEventListener

Nikita Rybak
i mean is there anything i can do to add this method to the document ?
Miroo
@Miroo No, but you can use IE-specific way to handle events. (like 'onload' in my example)
Nikita Rybak
and it works with me on IE9 but on a different application under framework 4
Miroo
unfortnally :S i am adding a new feature for IE9 which enable buttons in the thumbBar to do some JS functions so i have to attach that event 'msthumbnailclick'
Miroo
@Miroo And none of two provided here ways to attach handlers work?
Nikita Rybak
+1  A: 

If this code snippet returns false, this means that the addEventListener method property is not support by the browser. This is the case for Internet Explorer, where attachEvent is used instead:

if (document.addEventListener){  
  document.addEventListener(...);
} else if (document.attachEvent){  
  document.attachEvent(...);  
}
romaintaz
I am trying to use this to add a thumbBarButtons on IE9 and i tried the attachevent and it didn't work :S and all the code samples didn't mention that we can use it to attach the 'msthumbnailclick' event
Miroo
IE9 supports addEventListener
kennebec