Hi guys.
Recently I had face an interesting issue when trying on Hijacking Event on an Element here is what I've got:
So if you want to Hijack an Event first you have to check that event-handler has been set for that particular element or not rite ? ( you can't hijack if it has nothing there) so here is the piece of code :
function HijackEvent (aEl, aEvent, aFn)
{
for (var i in aEl)
{
if ((""+i) == aEvent)
{
if (typeof(aEl[i]) != undefined && aEl[i] != null) //this things was add cuz all the onXXXX properties on IE <8 (not yet check ie8) is existed event if the handler was not set
{
............
}
}
}
....
}
here is some scenarios which some become really nasty.
for most of us when we set a handler for element we used this for instance:
element.onclick = function (){blah blah ....};
this case was recognized by the first piece of code ... sweet ... keep going on
In some cases we want to set event handler for element directly in the html tag
<div id="aDIV" onclick =" doAsh1t();" />
I did not check this case on IE yet but with firefox (with the help of fire bug of course), when the element was inspected and the element event did not trigger (or fire) for the first time the property onclick of the element surprisingly did not existed on the properties list of the element not until the event onclick of the element was fired ... okei so this case I can handle by dig in the attribute properties of the element and god still love me so I still can find out that the event handler has been set for this element already ... but the Satan come then.
for some web apps properly with the extend of DOM2 we has a new way of adding event handler like this
element.addEventListener(evt,fn,false);
for IE it should be :
element.attachEvent('on'+evt,fn);
The problem is now the no fk*ng ways that I can find out if that element has been set event handler or not if the handler was set that way ... not a single trace in element properties or element attributes (surprisingly the sh1tty ie still push the handler function to element property )... and the element still has it event handlers set ... how come ?
So the question for me is how to check an Event Handler has already been set for the element (cross browsers and at least must work on firefox)
plz don't give me answer like
if(typeof someNode.onclick == "function") {
// someNode has an event handler already set for the onclick event...
}
cuz that algorithm based on the properties of element and if the element property was not there then the solution can't handle .