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

Discussion in 'Javascript' started by Robert S, Jul 29, 2006.

  1. Robert S

    Robert S Guest

    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.
    Robert S, Jul 29, 2006
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Jul 29, 2006
    #2
    1. Advertising

  3. Robert S

    Robert S Guest

    > 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.
    Robert S, Jul 29, 2006
    #3
  4. Robert S

    Philipp Guest

    Did you already try just returning false?

    function rejectEnter( oEvent )
    {
    if ( 13 == oEvent.keyCode )
    {
    try
    {
    oEvent.preventDefault();
    }
    catch ( oError )
    {
    oEvent.returnValue = false;
    }
    return false;
    }
    }
    Philipp, Jul 30, 2006
    #4
  5. Robert S

    RobG Guest

    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
    RobG, Jul 31, 2006
    #5
  6. Robert S

    Robert S Guest

    > 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.
    Robert S, Aug 1, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trung Chinh Nguyen
    Replies:
    5
    Views:
    454
    Trung Chinh Nguyen
    Jul 30, 2006
  2. Bob
    Replies:
    24
    Views:
    1,541
  3. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
  4. Macamba
    Replies:
    1
    Views:
    119
    Jim Ley
    Jan 17, 2004
  5. Severus Snape

    Other Opera incorrect behaviour

    Severus Snape, Jun 5, 2005, in forum: Javascript
    Replies:
    7
    Views:
    96
    Hallvord R. M. Steen
    Jun 9, 2005
Loading...

Share This Page