Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy firefox return key

    Hello everyone,
    I am very new to this forum as well as to javascript. I have following code for enter key event on my page. its working in all browsers except firefox. I have searched online a lot but nothing has worked and finally I have thought to get some help from the experts.

    <script>
    function checkKey()
    {
    if (window.event.keyCode == 13)
    {
    loadUrl();
    }
    return true;
    }
    </script>


    onkeydown="checkKey();return true;"

    any help is much appreciated.

    Thank you.

    Kind Regards.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    alert ("Keycode = " + key);
    
    if (key == 13) {
    alert ("You pressed the ENTER key");
    }
    
    }
    </script>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by raicha View Post
    its working in all browsers except firefox.
    window.event is specific to Internet Explorer 8 and earlier. Every other browser (including Internet Explorer 9) supports DOM2 Events, which means that you need to pass the event when the function is called.

    The following code does that while still supporting older versions of Internet Explorer:

    Code:
    onkeydown="if (typeof(event) === 'object') { checkKey(event); } else { checkKey(window.event); }"
    Code:
    function checkKey(event) {
    	if (event.keyCode === 13) {
    		loadUrl();
    	}
    }
    I removed instances of return true; since that code isn't doing anything.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    raicha (06-13-2012)

  • #4
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    solved like a charm.............

    @Philip M and Kitkat 007. when I used your code, it produced alert on every key press and shown every key code. when I removed alert ("Keycode = " + key); from it, it worked fine. But whenever I press enter button it runs the function doesn't matter where my cursor is. that is undesirable. I wanted to run the function only by pressing enter key if cursor is in text box. Thank you for helping out though.

    @Arbitrator Thank you very much. your code worked like a charm. It is exactly what I am looking for. I was using IE7 by mistake therefore previous code worked in IE too. thanks for letting me know about DOM2 support. you have made my night as I am going to sleep really well after solving this puzzle.
    Thank you.

    Kind Regards.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by Arbitrator View Post
    Code:
    onkeydown="if (typeof(event) === 'object') { checkKey(event); } else { checkKey(window.event); }"
    Since the IE window.event can simply be referenced as event you can simplify that down to:

    Code:
    onkeydown="checkKey(event)";
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by raicha View Post
    @Philip M and Kitkat 007. when I used your code, it produced alert on every key press and shown every key code. when I removed alert ("Keycode = " + key); from it, it worked fine.
    Well, yes. I would expect that you realised that the alert is simply for testing purposes.

    You did not say that you wanted it to work only when the cursor is in a textbox. You said "I have following code for enter key event on my page."


    Code:
    <input type = "text" id = "textfield" size = "20" onkeyup = "chk(event)">
    
    <script type = "text/javascript">
    
    function chk(ev) {
    var key;
    ev = ev || event;
    key = ev.keyCode;
    if (key == 13) {
    alert ("You pressed the Enter key");
    return false;
    }
    }
    
    </script>
    Last edited by Philip M; 06-13-2012 at 10:24 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by felgall View Post
    Since the IE window.event can simply be referenced as event you can simplify that down to:

    Code:
    onkeydown="checkKey(event)";
    Good catch.

    I guess that means that Philip M's code can be simplified too:

    Code:
    function chk(event) {
    	if (event.keyCode == 13) {
    		alert("You pressed the Enter key");
    		return false;
    	}
    }
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #8
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you everyone for great help. everyone's code is working really charm but I can only use one of them though. I didn't know that members of this forum are so much helpful. Really pleased to be a part of this community.
    Thanks everyone.


  •  

    Posting Permissions

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