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

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

So what this piece of code does is:

If e exists, keep e. If it doesn't exist, you are using an older version of IE and we assign the windows.event object to e. Now all browsers behave the same way.


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

To break it down, when an event handler is fired:

  • Some browsers pass a parameter to the callback holding event data (this is the standards-compliant way of doing it)
  • Other browsers (mostly old IE) instead put the event data in window.event (which is accessed here with just event, which is risky since that relies on there being no local variable with that name)

Next, e = e || event; is a standard way of saying "if the parameter was not passed, default it to whatever's after the ||". In this case, if the event parameter is not passed, then it looks for the global variable.

e.returnValue is one of three ways to stop an event from causing its default action. The other two are e.preventDefault && e.preventDefault() (which is conspicuously absent from the code you posted), and return false;