Connecting Tech Pros Worldwide Help | Site Map

addEventListener not working in mozilla

Newbie
 
Join Date: Sep 2006
Posts: 1
#1: Sep 25 '06
i have given the stmt as

Expand|Select|Wrap|Line Numbers
  1. document.addEventListener('onkeydown', my_onkeydown_handler, true);

and the function defined was

Expand|Select|Wrap|Line Numbers
  1. function my_onkeydown_handler()
  2. {
  3.     alert('called');
  4.     switch (event.keyCode)
  5.     {
  6.       case 116 : // 'F5'
  7.       event.returnValue = false;
  8.       event.keyCode = 0;
  9.       window.status = "We have disabled F5";
  10.       break; 
  11.     }
  12. }
  13.  
but it is not working
Newbie
 
Join Date: Feb 2008
Posts: 1
#2: Feb 6 '08

re: addEventListener not working in mozilla


The correct syntax is:
Expand|Select|Wrap|Line Numbers
  1. document.addEventListener('keydown', my_onkeydown_handler, true);
The DOM Level 2 event model doesn't follow the old naming convention of "oneventname". Note however that IE's attachEvent() method does require the event name to start with "on".
Dasty's Avatar
Expert
 
Join Date: Nov 2007
Location: Slovakia
Posts: 101
#3: Feb 6 '08

re: addEventListener not working in mozilla


^^ as Timo said. It works fine with mozilla browsers.

But then, you are using IE like event handling (working with window.event object) in your function. Mozilla is sending event object as parameter to even handler function (so alter your code). Yes, it's sad that you have to write different code for different browsers.

Example for cross-browser event handling:

Expand|Select|Wrap|Line Numbers
  1. if (document.addEventListener){
  2.   // as Timo suggested
  3.   document.addEventListener('keydown', my_onkeydown_handler, true); 
  4. } else if (document.attachEvent){
  5.   // IE style
  6.   document.attachEvent('onkeydown', my_onkeydown_handler);
  7. }
  8.  
  9. function my_onkeydown_handler(ev)
  10. {
  11.   ev = ev || window.event; // for IE
  12.   alert('keycode:' + ev.keyCode);
  13. }
or jsut simple use:
Expand|Select|Wrap|Line Numbers
  1. document.onkeydown = my_onkeydown_handler
if you dont plan to handle this event with more then one handler function.
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#4: Feb 6 '08

re: addEventListener not working in mozilla


You can run your event detection just once, and call the same assignment in either event model.

Expand|Select|Wrap|Line Numbers
  1. if(window.addEventListener){
  2.     addEve= function(who,wot,fun,boo){
  3.         return who.addEventListener(wot,fun,false)
  4.     }
  5. }
  6. else if(window.attachEvent){
  7.     addEve= function(who,wot,fun){
  8.         return who.attachEvent('on'+ wot,fun);
  9.     }
  10. }
For IE to 'hear' the keydown, attach the event to the body, not the document.
This will work as well in Firefox.
And don't set the capture to true on key events- the event will have come and gone before the capture returns a value. Capture only works in the Mozilla browsers, and it is rarely needed.
In any browser you can set your event handler on a parent element,up to the body, and test the target or srcElement of the event when it bubbles up.

addEve(document.body,'keydown', my_onkeydown_handler)
Reply