473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.

Jul 29 '06 #1
5 7303


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 29 '06 #2
Try to use preventDefault in onkeypress and not in onkeydown, that might
help.
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.

Jul 29 '06 #3
Did you already try just returning false?

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

Jul 30 '06 #4
Robert S wrote:
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).
--
Rob

Jul 31 '06 #5
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.

Aug 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: julio | last post by:
I noticed that pressing spacebar when a listbox has the focus scrolls the listbox to the first selected item. I'm intercepting the spacebar press event at form level for stopping and starting...
3
by: Lachlan Hunt | last post by:
Hi, I've been looking up lots of documentation and trying to work out a cross-platform method of capturing a keyboard event and working out which key was pressed. From what I can find, there...
4
by: Chris | last post by:
Hi, I think I'm having some problems here with garbage collection. Currently, I have the following code: public struct Event { public int timestamp;
7
by: Nikki | last post by:
Hi, Can anybody help me, i want to prevent windows to close my winform of ..NET application, when user presses Alt+F4
9
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
13
by: Perecli Manole | last post by:
In the forms authentication construct, I need a way to prevent ticket IssueDate and Expiration from being updated for a specific page only. By default forms authentication updates these two values...
16
by: MLH | last post by:
If I give someone a runtime app, they can open the database window by pressing the F-11 key. How to prevent???
6
by: Fnord Nase | last post by:
Hi. We're using the dblclick event in some parts of a web app interface, which works okay, but when a user double clicks on a text element, the browser selects some text before executing the...
4
by: helenwheelss | last post by:
Access 2003, using a bound form. I'm seeing rather annoying behaviour when editing data in a control with a default value. It only happens when the form is on a new record. A specific...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.