468,103 Members | 1,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Safari (Mac) 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
3 1797
Marcia Gulesian <mg@theworld.com> writes:
The following code suppresses the 'enter' key, when run in I.E. 5.5 or
later (Windows) but not when run in Safari (Mac)
.... or pretty much any non-IE browser.

<body onkeypress="javascript:keysuppress(event)" >
Just:
<body onkeypress="keysuppress(event)">
You almost never need to write "javascript:" in your pages.
A better way would be:
<body onkeypress="return keysuppress(event)">
Returning false from the handler is the most consistent way of
stopping an event.
function keysuppress(e)
{
if (e.type=="keypress" && e.keyCode=="13")
No need to test the event type when you are only called from an
"onkeypress" intrinsic event handler - it will be a key press.

The keyCode is a number, so it would be prettier to compare with the
number 13 instead of the string "13". And marginally more efficient,
if it mattered (it doesn't).
{
event.returnValue=false
Here you use "event" instead of the variable "e". IE makes the event
available as the global variable "event", but not all other browsers
do. The "returnValue" property of the event is also an IE invention.

To be consistent with the W3C DOM, you should call the method
"preventDefault" on the event (but obviously, IE doesn't have one).

For maximal consistency, you would write:

if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}

Or, with the addition of the "return" in the handler text above,
you could just return false:
---
function keysuppress(e) {
return (e.keyCode != 13);
}
---
What code can I use to suppress the 'enter' key when running an app in
Safari?


I don't have access to Safari, but I'll be surpriced if this doesn't work.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
Thank you Lasse.

Lasse Reichstein Nielsen wrote:
Marcia Gulesian <mg@theworld.com> writes:
The following code suppresses the 'enter' key, when run in I.E. 5.5 or
later (Windows) but not when run in Safari (Mac)


... or pretty much any non-IE browser.
<body onkeypress="javascript:keysuppress(event)" >


Just:
<body onkeypress="keysuppress(event)">
You almost never need to write "javascript:" in your pages.
A better way would be:
<body onkeypress="return keysuppress(event)">
Returning false from the handler is the most consistent way of
stopping an event.
function keysuppress(e)
{
if (e.type=="keypress" && e.keyCode=="13")


No need to test the event type when you are only called from an
"onkeypress" intrinsic event handler - it will be a key press.

The keyCode is a number, so it would be prettier to compare with the
number 13 instead of the string "13". And marginally more efficient,
if it mattered (it doesn't).
{
event.returnValue=false


Here you use "event" instead of the variable "e". IE makes the event
available as the global variable "event", but not all other browsers
do. The "returnValue" property of the event is also an IE invention.

To be consistent with the W3C DOM, you should call the method
"preventDefault" on the event (but obviously, IE doesn't have one).

For maximal consistency, you would write:

if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}

Or, with the addition of the "return" in the handler text above,
you could just return false:
---
function keysuppress(e) {
return (e.keyCode != 13);
}
---
What code can I use to suppress the 'enter' key when running an app in
Safari?


I don't have access to Safari, but I'll be surpriced if this doesn't work.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Jul 23 '05 #3
Lasse Reichstein Nielsen wrote:
Marcia Gulesian <mg@theworld.com> writes:
What code can I use to suppress the 'enter' key when running an app in
Safari?


I don't have access to Safari, but I'll be surpriced if this doesn't work.

^^^^^^^^^
Sorry for the public "spelling flame" but since you do not seem to read
your e-mails: It is _surprised_ and _surprise_. Seems to be a common
mistake among European students that to not have English as native
language, especially in Scandinavian countries (from what Google tells me).
HTH

\V/ PointedEars
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by sanj | last post: by
4 posts views Thread by Nitronic | last post: by
2 posts views Thread by laredotornado | last post: by
34 posts views Thread by Simon Wigzell | last post: by
16 posts views Thread by Edward | last post: by
7 posts views Thread by David Stone | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.