Basically, in Chrome and IE, the event object is global and can be referenced in an event handler (a method called when an event is triggered) without it being explicitly included as a parameter.
However, with Firefox, the event object must be explicitly passed as a formal (defined) parameter.
Overview
In IE and Chrome, the event object is global and available to the event handler whether it is defined as a formal parameter or not.
In Firefox 49, the event object is NOT global and must be explicitly passed - meaning that it must be explicitly added to the function definition. Depending on the method used to associate the event with a handler, the key word event either must be explicitly specified or it may be omitted.
In all browsers (that I tested), the key word event must be included when specifying an event handler via an html tag. (See details below.)
This page has code showing most of the possible combinations of implicit and explicit use of the event object and how to register the handlers - just look at the source.
html tags
<sometag id="m1" onmouseover="func_abc()">Call handler when the mouse is over this</sometag> <sometag id="m2" onmouseover="function(){func_abc();}">A lot more text for the same effect</sometag> |
<sometag id="m3" onmouseover="func_abc(event)">This handler can use the event object</sometag> <sometag id="m4" onmouseover="function(event){func_abc(event);}">A lot more text for the same effect</sometag> |
<sometag id="m3" onmouseover="func_abc(this)">This handler gets a reference to this object/tag</sometag> <sometag id="m4" onmouseover="function(this){func_abc(this);}">A lot more text for the same effect</sometag> |
Using code
mcSetEvent(document.getElementById('s1'), "mouseover", func_abc) ; |
function func_abc(event){ var target = event.target; // fails in IE8 target.value += 1; // or whatever you really want } |
mcSetEvent(document.getElementById('sefee'), "mouseover", function(event){func_abc(event);} ) ; |
This function handles the fact that there are 2 separate "standards" - IE and Chrome|Firefox|Edge - to define (associate) event handlers via code.
function mcSetEvent(UIElement, event_name, func_1) { if (UIElement.addEventListener) { UIElement.addEventListener(event_name, func_1) ; // Chrome, Firefox } else { UIElement.attachEvent("on"+event_name, func_1) ; // IE } } |
var u = function(t, a, b) { t.addEventListener ? t.addEventListener(a, b, !1) : t.attachEvent && t.attachEvent("on" + a, b) }; |
<sometag id="mox" onmouseover="func_abc(event)">Call handler when the mouse is over this</sometag> <sometag id="mox" onmouseover="function(event){func_abc(event);}">A lot more text for the same effect</sometag> |
Of course, if the event object is not needed, you can simply define the calls without any parameters.
By the way, if you want to associate an event handler that does not require an event object, you can use code similar to
document.getElementById('mox').onmouseover = func_xyz ; // function name with no parameters |
event.target fix
var target = mcTarget(event); function mcTarget(event){ if (document.addEventListener) { return event.target; }else{ return event.srcElement; // ie8 sucks } } |
var mcTarget ; // create a global variable if (document.addEventListener) { mcTarget = function(event){return event.target;} } else { mcTarget = function(event){return event.srcElement;} } |
The following table uses the mouseover event to test when the event object is available to the event handler. In some cases, the object is explicitly passed, in others, it is not. Color coding indicates the results I obtained.
| ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
No parentheses/parameters | With parentheses/parameters | function(){xx} | function(event){xx} | |||||||
onmouseover in tag | implicit | explicit | implicit() | explicit(event) | Nothing to test here | |||||
addEventListener | implicit | explicit | implicit() Calls the function Can never work | explicit(event) Calls the function Can never work | func(){implicit()} | func(){explicit(event)} | func(event){implicit()} | func(event){explicit(event)} | ||
attachEvent | implicit | explicit | func(){implicit()} | func(){explicit(event)} | func(event){implicit()} | func(event){explicit(event)} | ||||
mouseover in code | implicit | explicit | func(){implicit()} | func(){explicit(event)} | func(event){implicit()} | func(event){explicit(event)} | ||||
onmouseover in code | implicit | explicit | func(){implicit()} | func(){explicit(event)} | func(event){implicit()} | func(event){explicit(event)} | ||||
mcSetEvent | implicit | explicit | Nothing to test here | func(event){explicit(event)} |
Color key | ||||||
---|---|---|---|---|---|---|
All 3 | Chrome only | IE8 only | Chrome & Firefox | Chrome & IE8 | Aways fails |
function implicit(){ response.innerHTML = "This fails!"; if (event === undefined) return ''; response.innerHTML = "This works :)"; } function explicit(event){ response.innerHTML = "This fails!"; if (event === undefined) return ''; response.innerHTML = "This works :)"; } |
An event handler is a function associated with an event. Normally, a function is referred to by just its name - when is is followed by parentheses, then the function is executed and a result is returned. In Chrome, it does not matter if an event is attached to a function or to the "result" of a function, ie. it does not matter is the parentheses are present are not. Chrome simply assumes that you meant to specify the function and not execute it, it mostly ignores stuff that does not make sense and/or "fails gracefully".
UIElement.addEventListener(event_name, func_1) ; // This works in all browsers except IE8 UIElement.addEventListener(event_name, func_1()) ; // This works in Chrome, fails in Firefox UIElement.addEventListener(event_name, func_1(event)) ; // This works in Chrome, fails in Firefox UIElement.addEventListener(event_name, function(event){func_1(event)}) ; // This works in all browsers except IE8 |
There are a number of additional syntax combinations that work in Chrome that fail in one or more other browsers.
The point of this page was to determine what will work in all 3 browsers. This is it - it uses one method for Firefox and Chrome, and another for older IE versions.
mcSetEvent(UINumber, "mouseup", mcNumber_ButtonsUp ) ; //This works in all browsers including IE8 function mcSetEvent(UIElement, event_name, func_1) { if (UIElement.addEventListener) { UIElement.addEventListener(event_name, func_1) ; // Chrome, Firefox } else { UIElement.attachEvent("on"+event_name, func_1) ; // IE8 } } function mcNumber_ButtonsUp(event){ // This explicitly includes event var target = mcTarget(event); // Cross browser method // other code here } |
Notes
This code fragment works without failure in Chrome but fails with
event not found |
UINumber.addEventListener("mouseout", function(){ mcNumber_ButtonsUp(); }) ; function mcNumber_ButtonsUp(){ var target = event.target; } |
UINumber.addEventListener("mouseout", function(){ mcNumber_ButtonsUp(event); }) ; function mcNumber_ButtonsUp(event){ var target = event.target; } |
UINumber.addEventListener("mouseout", mcNumber_ButtonsUp ) ; function mcNumber_ButtonsUp(event){ // event object in function definition var target = event.target; } |
UINumber.addEventListener("mouseout", mcNumber_ButtonsUp ) ; function mcNumber_ButtonsUp(){ // event object NOT in function definition var target = event.target; } |
UINumber.addEventListener("mouseout", function(event){ mcNumber_ButtonsUp(event); }) ; function mcNumber_ButtonsUp(event){ var target = event.target; } |
UINumber.addEventListener("mouseout", mcNumber_ButtonsUp ) ; function mcNumber_ButtonsUp(event){ var target = event.target; } |