How do I prevent default behaviour for an event in Opera?

R

Robert S

I am using the following to prevent the Enter key from submitting my
form (this would have undesirable results):

(index.html)
<body onkeydown="rejectEnter( event )">

(functions.js)
function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
}
}

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?

It looks like it execultes the line "oEvent.preventDefault();", but it
does not have the desired effect.
 
M

Martin Honnen

Robert S wrote:

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?

Try to use preventDefault in onkeypress and not in onkeydown, that might
help.
 
R

Robert S

Try to use preventDefault in onkeypress and not in onkeydown, that might

This seems to work:

form action="recipients.php" method="post" onkeypress="rejectEnter(
event )">

I've experimented with a few things - its hard to know what
consistently works. Sometimes when I make a small change to
functions.js it stops working. Think I'll leave it alone now.
 
P

Philipp

Did you already try just returning false?

function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
return false;
}
}
 
R

RobG

Robert said:
I am using the following to prevent the Enter key from submitting my
form (this would have undesirable results):

Why not use onsubmit and a form validation routine?

(index.html)
<body onkeydown="rejectEnter( event )">

(functions.js)
function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
}
}

try..catch is really just a kludge where nothing better is available,
feature detection is all the rage so why not use it:

function rejectEnter(oEvent)
{
if ('number' == typeof oEvent.keyCode
&& 13 == oEvent.keyCode){
if ('function' == typeof oEvent.preventDefault){
oEvent.preventDefault();
} else {
oEvent.returnValue = false;
}
}
return false;
}

This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?

Presumably users are familiar with the fact that pressing 'enter' will
sumbit the form, why change the browser's default behaviour and confuse
them? Use an onsubmit handler and cancel the submit if they havn't
finished filling in the form.

It looks like it execultes the line "oEvent.preventDefault();", but it
does not have the desired effect.

Returning false from an onsubmit handler should work more reliably than
attempting to cancel default behaviour by intercepting key presses
(noting that client-side validation is helpful but unreliable).
 
R

Robert S

function rejectEnter(oEvent)
{
if ('number' == typeof oEvent.keyCode
&& 13 == oEvent.keyCode){
if ('function' == typeof oEvent.preventDefault){
oEvent.preventDefault();
} else {
oEvent.returnValue = false;
}
}
return false;
}

Thanks. This seems to work. The page is for internal office work, so
the fact that the Enter key doesn't work won't be a major problem.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top