Marcia Gulesian wrote:
The following code suppresses the 'enter' key, when run in I.E. 5.5 or
later (Windows) but not when run in Safari (Mac)
<body onkeypress="javascript:keysuppress(event)" >
function keysuppress(e)
{
if (e.type=="keypress" && e.keyCode=="13")
{
event.returnValue=false
}
}
What code can I use to suppress the 'enter' key when running an app in
Safari?
I don't have Safari here to test but the following hopefully works as it
uses both the W3C DOM Level 2 event handling preventDefault method and
the established return false to cancel the keypress event:
<html lang="en">
<head>
<title>keypress event handling</title>
<script type="text/javascript">
function init () {
document.onkeypress = function (evt) {
if (!evt && window.event) {
evt = window.event;
}
if (evt) {
var keyCode = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode : evt.which;
if (keyCode == 13) {
if (evt.preventDefault) {
evt.preventDefault();
}
return false;
}
else {
return true;
}
}
return true;
};
if (document.layers) {
document.captureEvents(Event.KEYPRESS);
}
}
init();
</script>
</head>
<body>
<form action="">
<p>
You should not be able to enter line breaks if the keypress event handling
works:
<br>
<textarea name="textareaName" rows="5" cols="80"Kibology for all.
</textarea>
</p>
</form>
</body>
</html>
Tested to work in IE 6, Netscape 7.1, Mozilla 1.7, Netscape 4.
Please report back to the group whether that solves the problem for Safari.
--
Martin Honnen
http://JavaScript.FAQTs.com/