Javascript code to disable enter key doesn't work in Netscape

Posted on 2003-11-19
Last Modified: 2008-02-26
The code below does not work in Netscape 7+ (does work in IE). How do I work around this?

   <script language="javascript" type="text/javascript">
          document.onKeyDown = function keyPress(evt)
               var keyCode =
               document.layers ? evt.which :
               document.all ? event.keyCode :
               document.getElementById ? evt.keyCode : 0;
               if (keyCode == 13)
                   return false;
Question by:lcftahoe
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 24

Accepted Solution

shivsa earned 500 total points
ID: 9782807
LVL 24

Expert Comment

ID: 9782840

To capture keystrokes, it's a little different in IE and Netscape (of course). Here is code which will work in either browser:

document.onkeypress = getKey;
function getKey(keyStroke) {
   var keyCode = (document.layers) ? keyStroke.which : event.keyCode;
   var keyString = String.fromCharCode(keyCode).toLowerCase();

Your form's onKeyDown event should then simply call getKey() to enter the function.
LVL 27

Expert Comment

by:Asta Cu
ID: 10376647
No response here, so can only assume that shivsa has helped and the solution is to be accepted.  Will check back shortly and post Moderator recommendation.
LVL 27

Expert Comment

by:Asta Cu
ID: 10398047
No comment has been added lately (2 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to shivsa http:#9782840

Please leave any comments here within 4 days.

-- Please DO NOT accept this comment as an answer ! --

EE Cleanup Volunteer

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had to do a bit of research to find the answer to this question so I thought I'd share my results.  Due to our outdated mainframe systems, we need to downgrade IE9 to IE8 in order to stay compatible.  We also needed to downgrade Java.  In order to…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question