The following code is from ‘JavaScript by Example Second Edition’ EXAMPLE 15.24
I don’t understand the code var evt = e || window.event; //Browser differences
I think the var evt should be true or false, so evt.target should be null, right?
function colorText(e){
var evt = e || window.event; //Browser differences
var evtTarget= evt.target || evt.srcElement;
if(evtTarget.id=="first"){
div1.className="red";
}
else if(evtTarget.id == "second"){
div2.className="blue";
}
else{ div3.className="green";}
}
The
keywordexpression returns the value of the firstexpressionoperand if it’s notnullfalsey, otherwise the value of the second.Javascript is weak typed, so you can always use || as a boolean, because everything which isn’t false or null evaluates to true. But it’s actually returning the value here, s. Soevtbecomes eithere(the argument) or ifedoesn’t existhas a falsey value, it becomeswindow.event.