When an event is set from a HTML tag, Mozilla hides the difference between the two schemes.
<a id="test" href="" onclick="runOnClick(event)"> <script> alert( document.getElementById( "test")); </script>
generates different implicit JavaScript depending on the platform. Mozilla will create:
function( event) { runOnClick(event); }
While IE will generate:
function() { runOnClick(event); }
This is quite clever. This means that events set in HTML tags can all get the event in the same way.
Events set on an element by JavaScript is not so lucky. They have to account for the different platforms:
document.getElementById( "test").onclick = function( evt)
{
evt = evt || event;
...
}
This is the simplest working method for retrieving the event object. We try and pass it in. If the event caller does not have an argument we use the global event object used by IE. For this reason we have to call the argument evt so as not to hide the global one called event.
evt.target = evt.target || evt.srcElement;
There are a number of ways around this. If the event is set in the HTML you can call a function giving the ID of the calling element. More general code could walk up from the target element until it finds an element with the correct event. The most general solution uses some of the more obtuse properties of the JavaScript OO system to set the owner. If the event is set functionally, you can have an inner function that refers to the element being set that is an argument to the outer function. This becomes a dirty circular reference - so that the event data is not released when the outer function returns - but it does the job. This last method isn't obvious, so perhaps an example will clear it up. The code calls an action method with a normalised event object. Note that the element argument for the add function is used inside a event function that can be called at any arbitrary time later. It's magic.
Events.add = function( element, name, action)
{
...
element["on"+name] = function( evt)
{
evt = evt || event;
evt.target = evt.target || evt.srcElement;
evt.owner = element;
action( evt);
}
}