www.webdeveloper.com
Results 1 to 14 of 14

Thread: cant get keycode from keydown event in firefox

  1. #1
    Join Date
    Apr 2006
    Posts
    76

    Exclamation cant get keycode from keydown event in firefox

    Hi!
    Can anyone tell me what's wrong with the following code. I cant get the keycode although the keydown event is triggered . In firefox JS console , I saw e has no property . this code worked well in IE and Opera . Thx

    function kd(e)
    {
    var intKey = 0;
    if (window.event)
    intKey = event.keyCode;
    else
    intKey = e.which;

    if (intKey == 13) //enter key
    alert(13);
    }
    thx alots

  2. #2
    Join Date
    Aug 2005
    Location
    Northampton, UK.
    Posts
    421
    i had the same problem a while ago.. i cant rememeber how i got past it... but if i can find the script i'll tell you....... cant you use charCode aswell?

    oh and e.which is the mouse button if thats what you're going for.
    Last edited by jam; 05-31-2006 at 07:58 AM.

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    change the else phrase to:

    else intkey= (e.keyCode)? e.keyCode: e.charCode;

    in the non-ie browsers the keypress event has a charCode;
    and the keyup and keydown events have a keycode;

    ie uses keyCode for all so you can safely write:

    Code:
    function kd(e){
    	var intKey = 0;
    	e= (window.event)? event : e;
    	intKey = (e.keyCode)? e.keyCode: e.charCode;
    	if (intKey == 13) alert(13);
    }
    which is only the keycode on Netscape 4-
    it has a different purpose in modern browsers, and is not associated with a keypress: e has no property['which'];
    Last edited by mrhoo; 05-31-2006 at 01:00 PM.

  4. #4
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by Whoknow
    I saw e has no property.
    This would seem to indicate a problem with how you've associated the handler with the event. That error message means that the event block was not passed to your handler. Show the associated HTML.

  5. #5
    Join Date
    Feb 2006
    Posts
    2,927
    I believe the error is
    e has no property['which'];

    which is only the keycode on Netscape 4-
    it has a different purpose in modern browsers, and is not associated with a keypress

  6. #6
    Join Date
    Apr 2006
    Posts
    76
    Quote Originally Posted by mrhoo
    change the else phrase to:

    else intkey= (e.keyCode)? e.keyCode: e.charCode;

    in the non-ie browsers the keypress event has a charCode;
    and the keyup and keydown events have a keycode;

    ie uses keyCode for all so you can safely write:

    Code:
    function kd(e){
    	var intKey = 0;
    	e= (window.event)? event : e;
    	intKey = (e.keyCode)? e.keyCode: e.charCode;
    	if (intKey == 13) alert(13);
    }
    which is only the keycode on Netscape 4-
    it has a different purpose in modern browsers, and is not associated with a keypress: e has no property['which'];

    I've tried your code but it still not working

  7. #7
    Join Date
    Apr 2006
    Posts
    76

    Smile

    Quote Originally Posted by phpnovice
    This would seem to indicate a problem with how you've associated the handler with the event. That error message means that the event block was not passed to your handler. Show the associated HTML.
    here is my HTML event handler
    <input id="Text1" type="text" onkeydown="kd()"/>

  8. #8
    Join Date
    Dec 2004
    Posts
    8,637
    Code that this way:

    <input id="Text1" type="text" onkeydown="return kd(event)"/>

    Then, your function can be coded as follows:
    Code:
    function kd(e)
    {
        var intKey = (window.Event) ? e.which : e.keyCode;
        if (intKey == 13) { //enter key
            alert(13);
            return false;
        }
        return true;
    }

  9. #9
    Join Date
    Apr 2006
    Posts
    76
    Quote Originally Posted by phpnovice
    Code that this way:

    <input id="Text1" type="text" onkeydown="return kd(event)"/>

    Then, your function can be coded as follows:
    Code:
    function kd(e)
    {
        var intKey = (window.Event) ? e.which : e.keyCode;
        if (intKey == 13) { //enter key
            alert(13);
            return false;
        }
        return true;
    }
    yes , it works now , thx a lots
    But can you tell me what different between my handler and yours . Why mine only works in IE & Opera

  10. #10
    Join Date
    Dec 2004
    Posts
    8,637
    The IE event object is global -- that is why it worked:

    window.event

  11. #11
    Join Date
    Apr 2006
    Posts
    76
    oh , now I see.
    one more question please: doese firefox support onerror event for image tag. It's not working. I find a work round on google , they use setTimeout technique . r any better workrounds out there?

  12. #12
    Join Date
    Dec 2004
    Posts
    8,637
    I'm not 100% certain. I've never tried to use it for an actual IMG tag -- only for the JavaScript Image() object (which does work in Firefox, too).

  13. #13
    Join Date
    Apr 2006
    Posts
    76
    yes , the image object works in firefox . I never thought about it before , just keep concetrating on the Img tag

    thx alots .

    do you familiar with Opera , I get an XML DOM manipulation problem in opera

    Can you tell me what wrong with my javascript when using appendChild method
    why I get this error : Unhandled exception: [Object DOMException]

    here is my code

    tmpNode = aRecord.getElementsByTagName("userName");
    tmpNode[0].appendChild(aXMLDoc.createTextNode("newUser"));

    The userName tag is existed in aRecord . and the tmpNode[0] also exist . when alert(tmpNode[0]); , it showed [object HTMLElement] .


    this code works well in firefox & IE


    Thx

  14. #14
    Join Date
    Dec 2004
    Posts
    8,637
    Nope, sorry, no familiarity with Opera.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles