Preventing onkeypress events in Netscape 7 for Mac fail

J

Jonas

Hi!

I have a web page where I want to intercept keypress events in an INPUT-tag
and check if it is the Enter key, which calls another function that executes
a search. My code runs on Netscape 7 for Windows, IE 6 for Windows and IE5.1
for Mac, but not on Netscape 7 for Mac. When I press the Enter key, the
event gets caught but it does not get prevented and the search is never
executed.

The code looks like this:

function KeyDownHandler(e)
{
// if not Netscape, get IE event
if ( !e )
e = window.event;
if ( !e )
return true;

// Get valid ascii character code
var key = typeof e.keyCode != 'undefined' ? e.keyCode : e.charCode;
// process only the Enter key
if (key == 13)
{
// cancel the default submit
if (e.preventDefault)
e.preventDefault();
else
window.event.returnValue=false;

// submit the form by programmatically searching
search();
return false;
}
else
{
return true;
}
}

TIA

Jonas
 
M

Martin Honnen

Jonas said:
I have a web page where I want to intercept keypress events in an INPUT-tag
and check if it is the Enter key, which calls another function that executes
a search. My code runs on Netscape 7 for Windows, IE 6 for Windows and IE5.1
for Mac, but not on Netscape 7 for Mac. When I press the Enter key, the
event gets caught but it does not get prevented and the search is never
executed.

The code looks like this:

function KeyDownHandler(e)

You can only cancel keys with Netscape/Mozilla for the keypress event. I
don't know how you use that function named KeyDownHandler but the name
suggests you are using it as the onkeydown handler while you should use
it as the onkeypress event handler.
Other than that I see nothing wrong, you might want to download a recent
Mozilla build from http://www.mozilla.org/ and check on the Mac, if the
problem persists you can file a bug report on https://bugzilla.mozilla.org/.
 
M

McKirahan

Jonas said:
Hi!

I have a web page where I want to intercept keypress events in an INPUT-tag
and check if it is the Enter key, which calls another function that executes
a search. My code runs on Netscape 7 for Windows, IE 6 for Windows and IE5.1
for Mac, but not on Netscape 7 for Mac. When I press the Enter key, the
event gets caught but it does not get prevented and the search is never
executed.

The code looks like this:

function KeyDownHandler(e)
{
// if not Netscape, get IE event
if ( !e )
e = window.event;
if ( !e )
return true;

// Get valid ascii character code
var key = typeof e.keyCode != 'undefined' ? e.keyCode : e.charCode;
// process only the Enter key
if (key == 13)
{
// cancel the default submit
if (e.preventDefault)
e.preventDefault();
else
window.event.returnValue=false;

// submit the form by programmatically searching
search();
return false;
}
else
{
return true;
}
}

TIA

Jonas

How are you invoking your function?

Is it like the following?

<input type="text" onkeypress="return KeyDownHandler()">
 
J

Jonas

McKirahan said:
How are you invoking your function?

Is it like the following?

<input type="text" onkeypress="return KeyDownHandler()">

Hi!

I invoke it like this:

<input type="text" onkeypress="KeyDownHandler(event);">

Could it be the missing return statement that does the trick?

Brgds

Jonas
 
M

McKirahan

Jonas said:
"McKirahan" <[email protected]> wrote in message

Hi!

I invoke it like this:

<input type="text" onkeypress="KeyDownHandler(event);">

Could it be the missing return statement that does the trick?

Brgds

Jonas

Yes. The "return " negates the Enter key when the function returns "false".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top