Search code examples
javascriptinternet-explorerattachevent

IE7: call is undefined on document.attachEvent function


I was trying to call .call Function prototype method on document.attachEvent in IE7. But it is showing as undefined. I have also checked typeof document.attachEvent, is should show as function but it is object. Can someone explain it why?

document.attachEvent.call(this, 'onclick', function () {});

Getting this error

Error: Object doesn't support this property or method

Solution

  • Host-provided functions are not required to be proper JavaScript functions, so long as they can be called. That means they aren't required to inherit from Function.prototype (and thus may not have call or apply, which come from Function.prototype) and typeof may not identify them as functions (because from a JavaScript perspective, while they're callable, they're not functions).

    Not all obsolete browsers made functions proper functions, including IE7.