A lot has changed with regard to events and event handling since the old days of HTML. Let’s have a brief look at it.
In the old days, it was common to add event listeners in one of two ways:
Or in the HTML declaration of an element:
The problem with the first approach was that different browsers would pass the event argument (Event class) in different ways:
Internet Explorer had a global window.event instance;
Other browsers would pass the event instance as an argument to the handler function.
One way to go around this would be something like:
But for the second one, only IE supported accessing the event instance, through the global window.event property. For other browsers, this was lost.
Either way, you could only add a single event listener for each event, which was a big limitation. IE did offer a proprietary solution to this, through the attachEvent method:
Likewise, the way to get the event was through window.event.
As for canceling the default behavior of the event (such as preventing browser navigation or form submittal), returning false usually worked, but it would mean that you had to declare the event handler appropriately, while using the inline HTML syntax:
In IE, setting window.event.returnValue to false would do the same.
Most browsers would use the dispatchEvent method for raising an event:
The difference between these two approaches is that the first allows having multiple event handlers for the same event, whereas the latter does not. As you can imagine, it is safer to go the addEventListener way, which, BTW, is what jQuery and other libraries use.
In HTML inline event handler declarations, it is now possible to access the event attribute through the arguments collection:
As for canceling an event, we now have the preventDefault method that does just the same.
Raising an event is now achieved through the dispatchEvent method:
Things around HTML are getting considerably better, with more commitment from browser manufacturers to standards. Whenever possible, do use them, or, better still, use jQuery and forget about it!