tags:

views:

21

answers:

1

I would like to know why this page's source use all this if-else to add an event listener:

if (document.addEventListener)
{
   document.addEventListener("keydown",keydown,false);
   document.addEventListener("keypress",keypress,false);
   document.addEventListener("keyup",keyup,false);
   document.addEventListener("textInput",textinput,false);
}
else if (document.attachEvent)
{
   document.attachEvent("onkeydown", keydown);
   document.attachEvent("onkeypress", keypress);
   document.attachEvent("onkeyup", keyup);
   document.attachEvent("ontextInput", textinput);
}
else
{
   document.onkeydown= keydown;
   document.onkeypress= keypress;
   document.onkeyup= keyup;
   document.ontextinput= textinput;   // probably doesn't work
}

Is it to be compatible with most browsers?

+1  A: 
if (document.addEventListener) // DOM spec that all browsers should follow

else if (document.attachEvent) // but unfortunately IE does not follow them    

else // and there may be other older browsers that follow neither
Anurag
this comments are perfectly coherent with your photo.. XD Thanks!
Tom Brito
thankyou, i take that as a compliment :D <- btw, that's most coherent with my profile pic
Anurag