468,107 Members | 1,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Safari (Mac) - keysuppress(event) issue


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?

Thanks.

Jul 23 '05 #1
2 1839


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/

Jul 23 '05 #2
Martin Honnen wrote:

What code can I use to suppress the 'enter' key when running an app in
Safari?

<snip>
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.


Doesn't work in Safari 1.0.2, no errors.
Mick
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by sanj | last post: by
3 posts views Thread by Marcia Gulesian | last post: by
3 posts views Thread by Marcia Gulesian | last post: by
7 posts views Thread by aljamala | last post: by
5 posts views Thread by dvpnet | last post: by
1 post views Thread by laltvm | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.