In a function, I had to add an event to an element called newElement
. To cater for different browsers like IE and Firefox, I checked the condition based on the attachEvent
property (only works in IE).
if ( typeof(newElement.attachEvent) != "undefined" )
{
newElement.attachEvent("onclick", deleteRow) ;
newElement.attachEvent("onclick", customDeleteScript) ;
}else
{
newElement.addEventListener("click", deleteRow, false) ;
newElement.addEventListener("click", customDeleteScript, false) ;
}
The original requirement was for deleteRow
to run before customDeleteScript
. This setup worked correctly in Firefox and Chrome but not in IE where customDeleteScript
executed first. In order to fix this issue, I had to rearrange the event additions as shown below:
if ( typeof(newElement.attachEvent) != "undefined" )
{
newElement.attachEvent("onclick", customDeleteScript) ;
newElement.attachEvent("onclick", deleteRow) ;
}else
{
newElement.addEventListener("click", deleteRow, false) ;
newElement.addEventListener("click", customDeleteScript, false) ;
}
The question arises whether this behavior is specific to IE or if it's just a random occurrence with IE always?
EDIT: What happens if my function has parameters like this
and others, and I'm unsure which function requires which parameters.