Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 215
Joro Seksa Avatar asked Mar 20 '13 23:03

Joro Seksa


People also ask

What is E in event handler?

The e parameter of the function is an optional parameter of the input event handler which equals to a JavaScript Event object that contains information regarding what action or event just happened.

What is the purpose of the event handlers in the JavaScript?

Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.


1 Answers

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;

like image 150
Niet the Dark Absol Avatar answered Sep 30 '22 01:09

Niet the Dark Absol