15-Apr-05 (Created: 15-Apr-05) | More in 'CS-JavaScript'

How to capture the enter key on a browser page

I have borrowed the following comp.lang.javascript post. The author of this is at the bottom of the post

<HTML> 
<HEAD> 
<SCRIPT> 

var message = 'ENTER disabled, click the button, stupid.'; 

if (document.layers) { 
  document.captureEvents(Event.K�EYPRESS); 
  document.onkeypress = NAVintercept; 
} 

function NAVintercept(e) { 
  if (e.which == 13) { 
    alert (message); 
    return false; 
  } 
  else routeEvent(e); 
} 

function EXPintercept() { 
  if (window.event.keyCode == 13) { 
    alert (message); 
  } 
} 

</SCRIPT> 
</HEAD> 
<BODY onKeyPress="EXPintercept()"> 
  <FORM ACTION="blah.cgi" METHOD=POST> 
    <P><INPUT TYPE=text NAME=name VALUE="" SIZE=30></P> 
    <P><INPUT TYPE=submit NAME=Submit VALUE="Submit"> 
  </FORM> 
</BODY> 
</HTML> 

This works on Navigator 4.72 and Explorer 5.01 for the Mac. It should work on equivalent browsers on the Dark Side as well. Post back to this group and let us know if it works for you, and what browser you were using.

Henry.