Why does this event handler use "e = e || event"?
cross-browser, dom-events, event-handling, javascript
Solution
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;`
Problem
Can someone explain me what does this line of code means: ``` function(e) { e = e || event; e.returnValue = false; return false; } ``` Why is the parameter named `e`? If I change it to 'myparam' will it work? What does `e = e` mean? Where is the variable `event` ( after `||` ) declared ? What is `e.returnValue?`