addEventListener not working in mozilla

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arif Mohd
    New Member
    • Sep 2006
    • 1

    addEventListener not working in mozilla

    i have given the stmt as

    [CODE=javascript]document.addEve ntListener('onk eydown', my_onkeydown_ha ndler, true);[/CODE]


    and the function defined was

    [CODE=javascript]function my_onkeydown_ha ndler()
    {
    alert('called') ;
    switch (event.keyCode)
    {
    case 116 : // 'F5'
    event.returnVal ue = false;
    event.keyCode = 0;
    window.status = "We have disabled F5";
    break;
    }
    }
    [/CODE]
    but it is not working
    Last edited by gits; Feb 6 '08, 06:40 PM. Reason: added code tags
  • timo1978
    New Member
    • Feb 2008
    • 1

    #2
    The correct syntax is:
    Code:
    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".

    Comment

    • Dasty
      Recognized Expert New Member
      • Nov 2007
      • 101

      #3
      ^^ 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:

      [CODE=javascript]if (document.addEv entListener){
      // as Timo suggested
      document.addEve ntListener('key down', my_onkeydown_ha ndler, true);
      } else if (document.attac hEvent){
      // IE style
      document.attach Event('onkeydow n', my_onkeydown_ha ndler);
      }

      function my_onkeydown_ha ndler(ev)
      {
      ev = ev || window.event; // for IE
      alert('keycode: ' + ev.keyCode);
      }[/CODE]

      or jsut simple use:
      Code:
      document.onkeydown = my_onkeydown_handler
      if you dont plan to handle this event with more then one handler function.

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

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

        Code:
        if(window.addEventListener){
        	addEve= function(who,wot,fun,boo){
        		return who.addEventListener(wot,fun,false)
        	}
        }
        else if(window.attachEvent){
        	addEve= function(who,wot,fun){
        		return who.attachEvent('on'+ wot,fun);
        	}
        }
        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_ha ndler)

        Comment

        Working...