Safari (Mac) - keysuppress(event) issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcia Gulesian

    #1

    Safari (Mac) - keysuppress(event) 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="jav ascript:keysupp ress(event)" >

    function keysuppress(e)
    {
    if (e.type=="keypr ess" && e.keyCode=="13" )
    {
    event.returnVal ue=false
    }
    }

    What code can I use to suppress the 'enter' key when running an app in
    Safari?

    Thanks.

  • Martin Honnen

    #2
    Re: Safari (Mac) - keysuppress(eve nt) issue



    Marcia Gulesian wrote:[color=blue]
    > 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="jav ascript:keysupp ress(event)" >
    >
    > function keysuppress(e)
    > {
    > if (e.type=="keypr ess" && e.keyCode=="13" )
    > {
    > event.returnVal ue=false
    > }
    > }
    >
    > What code can I use to suppress the 'enter' key when running an app in
    > Safari?[/color]

    I don't have Safari here to test but the following hopefully works as it
    uses both the W3C DOM Level 2 event handling preventDefault method and
    the established return false to cancel the keypress event:

    <html lang="en">
    <head>
    <title>keypre ss event handling</title>
    <script type="text/javascript">
    function init () {
    document.onkeyp ress = function (evt) {
    if (!evt && window.event) {
    evt = window.event;
    }
    if (evt) {
    var keyCode = evt.keyCode ? evt.keyCode :
    evt.charCode ? evt.charCode : evt.which;
    if (keyCode == 13) {
    if (evt.preventDef ault) {
    evt.preventDefa ult();
    }
    return false;
    }
    else {
    return true;
    }
    }
    return true;
    };
    if (document.layer s) {
    document.captur eEvents(Event.K EYPRESS);
    }
    }

    init();
    </script>
    </head>
    <body>
    <form action="">
    <p>
    You should not be able to enter line breaks if the keypress event handling
    works:
    <br>
    <textarea name="textareaN ame" rows="5" cols="80"[color=blue]
    >Kibology for all.[/color]
    </textarea>
    </p>
    </form>
    </body>
    </html>

    Tested to work in IE 6, Netscape 7.1, Mozilla 1.7, Netscape 4.
    Please report back to the group whether that solves the problem for Safari.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Mick White

      #3
      Re: Safari (Mac) - keysuppress(eve nt) issue

      Martin Honnen wrote:

      [color=blue][color=green]
      >> What code can I use to suppress the 'enter' key when running an app in
      >> Safari?[/color]
      >[/color]
      <snip>[color=blue]
      >
      > Tested to work in IE 6, Netscape 7.1, Mozilla 1.7, Netscape 4.
      > Please report back to the group whether that solves the problem for Safari.
      >[/color]

      Doesn't work in Safari 1.0.2, no errors.
      Mick

      Comment

      Working...