Quick question about JavaScript event objects – how does JavaScript know when I’m trying to pass an event variable to a function? For example, when I define an onclick handler like so:
<button onclick='SendMessage(event)'></button>
Does JavaScript pass the event variable just because I named it "event"? If so, what other variable names does it reserve for the purpose of passing event variables? (e? ev? etc..)
If this is the case, can they be covered by naming local or global variables "event", "e", etc.. ?
It must be called
eventin an inline event.eventis not a keyword and is not a reserved word in JavaScript; it is merely the variable name the early Netscape engineers decided upon.The inline text supplied for the event is effectively wrapped 1 as:
IE uses the
window.eventproperty to pass event information, but the names coincide so usingeventwill fallback through the normal JavaScript variable resolution as appropriate.Of course, if attaching a function object directly then the event variable, because it’s just the first parameter, can be named whatever is desired. Unfortunately IE’s
window.eventapproach must be dealt with as well, and calling the argumenteventdoesn’t address it.(I would recommend avoiding inline events as much as possible and using a library that unifies/simplifies events.)
Happy coding!
1 This behavior is covered in detail in HTML 5: Event Handlers, Event handler content attributes: