Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

firefox enter key 1

Status
Not open for further replies.

raicha

Technical User
Jun 12, 2012
2
0
0
US
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 things to be aware of.

1. Firefox does not handle the window.event object implicitly like other browsers.
2. Always check the browsers Error Console. when coding, it will show you any errors you may have n your JS code. It would have shown that window.event is undefined.

To make it work for firefox, you need to explicitly send the event object into the function.

Code:
onkeydown="checkKey([red][b]event[/b][/red]);return true;"

And then use it inside:

Code:
function checkKey([red][b]e[/b][/red])
{
 //check if window.event exists
 if(window.event)
 {
   if (window.event.keyCode == 13)
   {
     loadUrl();
   }
 }
 // if it doesn't use firefox code
 else
 {
   if(e.keyCode == 13)
   {
     //do stuff
   }
 }
return true;
}






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you vacunita. your suggestion is really helpful. Your code is working like a charm. Thank you very much. I really really appreciate it.

Kind Regards,
Amit Raichandani.
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank vacunita
and star this post![/navy]


at the bottom of vacunita's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top