Why does this event handler use the "e = e || event"?

Can someone explain to me what this line of code means:

function(e) { e = e || event; e.returnValue = false; return false; } 

Why a parameter named e ?
If I change it to "myparam", will it work?
What does e = e mean?

Where is the event variable declared (after || )? What is e.returnValue?

+11
source share
3 answers

This is all basic event management, although e.preventDefault() missing ...

To break it, when you start the event handler:

  • Some browsers pass a parameter for callback hold event data (this is a way to comply with standards)
  • Other browsers (mostly old IEs) instead place the event data in window.event (which is accessed here only with event , which is risky because it relies on the absence of a local variable with this name)

Next, e = e || event; e = e || event; is the standard way of saying "if this parameter has not been passed, it defaults to after || ". In this case, if the event parameter is not passed, it searches for a global variable.

e.returnValue is one of three ways to stop an event from its default action. The other two are: e.preventDefault && e.preventDefault() (which is clearly not in the code you sent), and return false;

+16
source

This line is only for IE8 and below to function the same as all other browsers. All other browsers pass the target element ( e ) to the event function.

So what this piece of code does:

If e exists, hold e . If it does not exist, you use an older version of IE, and we assign the windows.event e object. Now all browsers behave the same.

+2
source

This piece of code checks to see if the e object exists, otherwise use the event object and assign it to the e element. After that, the returnValue attribute is returnValue to false and returns false.

This is code that works the same in IE and other browsers, regardless of whether the event object is called e or event .

+1
source

All Articles