Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2012, 05:38 AM   PM User | #1
raicha
New to the CF scene

 
Join Date: Jun 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
raicha is an unknown quantity at this point
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.
raicha is offline   Reply With Quote
Old 06-13-2012, 07:33 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Old 06-13-2012, 07:54 AM   PM User | #3
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,887
Thanks: 5
Thanked 186 Times in 183 Posts
Arbitrator is on a distinguished road
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.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Users who have thanked Arbitrator for this post:
raicha (06-13-2012)
Old 06-13-2012, 08:46 AM   PM User | #4
raicha
New to the CF scene

 
Join Date: Jun 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
raicha is an unknown quantity at this point
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.
raicha is offline   Reply With Quote
Old 06-13-2012, 09:37 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 06-13-2012, 10:21 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
__________________

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.

Last edited by Philip M; 06-13-2012 at 10:24 AM..
Philip M is online now   Reply With Quote
Old 06-13-2012, 12:26 PM   PM User | #7
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,887
Thanks: 5
Thanked 186 Times in 183 Posts
Arbitrator is on a distinguished road
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;
	}
}
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 06-14-2012, 05:13 AM   PM User | #8
raicha
New to the CF scene

 
Join Date: Jun 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
raicha is an unknown quantity at this point
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.
raicha is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:00 AM.


Advertisement
Log in to turn off these ads.