What does the following mean in JavaScript?
var evt=event||window.event;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means that the variable
evtis assigned to the value ofeventor ifeventis undefined it is assigned the value ofwindow.event.How this works is that in javascript, boolean operators don’t evaluate to true or false but instead evaluates to the value of the last object that is not falsy* or the falsy value.
So the statement first evaluates the expression
event || window.event. Ifeventis true then the expression does not need to be evaluated any further since an OR only needs one member to be true. Therefore the value ofeventis returned. Ifeventis falsy then the the right side of the OR operator needs to be evaluated to determine if the result is false. In which case, ifwindow.eventis not falsy then its value is returned.This is a very common idiom to get the event object from event handlers. On standards compliant browsers, the event object is passed as the first parameter to the event handler. But on IE the event object is a global variable. And for historical reasons, all global variables are members of the window object.
So the code should look something like this:
Note: * falsy values in javascript are: false, 0, null and undefined.