Monday, October 10, 2011

addEventListener vs attachEvent vs jquery bind

Unfortunately IE is not friendly to this idea (addEventListener) and decide to throw a javascript error.

After dig around, I found that we need to use AttachEvent insteead of AddEventListener to support IE.

Here are some of the reference
https://developer.mozilla.org/en/DOM/element.addEventListener
http://msdn.microsoft.com/en-us/library/ms536343(VS.85).aspx

as suggested solution, I did a fix in couple javascript code to fix this issue.

if (el.addEventListener){
el.addEventListener(‘click’, functionText, false);
} else if (el.attachEvent){
el.attachEvent(‘onclick’, functionText);
}

why not using Jquery Bind ?
and here is better solution if you use Event capture in addEventListener
Unfortunately, Event capturing is not supported by jQuery, as event capturing is not supported by IE, which jQuery supports.

if (el.addEventListener){
el.addEventListener(‘click’, functionText

No comments: