Safari enter/return bypassing my button script

Discussion in 'Javascript' started by Tim Streater, Jun 12, 2007.

  1. Tim Streater

    Tim Streater Guest

    I have a form and a button to submit it. The button is made from:

    <input type=button onclick='myHandler(this.form);'>

    This all works fine except that in Safari 2.0.4, the enter/return keys,
    if pressed, submit the form - bypassing my onclick handler.

    I can partially fix this with:

    <form onsubmit='return false;'>

    except that if at the time enter/return is pressed, the focus is on one
    of the iframes in my form, then the iframe gets reloaded (with goofy
    parameters, of course). If the focus is nowhere when enter/return is
    pressed, then these get ignored OK.

    Note that with Firefox Mac and any WIndows browser, return seems to be
    ignored.

    How can I prevent Safari from submitting the form when enter/return are
    pressed?

    Thanks,
    Tim Streater, Jun 12, 2007
    #1
    1. Advertising

  2. Tim Streater

    Tim Streater Guest

    In article <Xns994D797BF87D0sonicyouth@216.196.97.131>,
    Good Man <> wrote:

    > Tim Streater <> wrote in
    > news::
    >
    > > I have a form and a button to submit it. The button is made from:
    > >
    > > <input type=button onclick='myHandler(this.form);'>
    > >
    > > This all works fine except that in Safari 2.0.4, the enter/return
    > > keys, if pressed, submit the form - bypassing my onclick handler.
    > >
    > > I can partially fix this with:
    > >
    > > <form onsubmit='return false;'>

    >
    > First off, though this isn't the reason for your problem, you can't expect
    > javascript to work properly if your (X)HTML markup is bad. Your button
    > script should be <input type="button" onclick="myHandler(this.form);">
    > (quotes around the input type).


    Are you saying that these:

    <form onsubmit='return false;'>
    <form onsubmit="return false;">

    are not equivalent?

    > That aside, why can't you put the myHandler function on the form submit?
    > Will that solve the Safari problem?
    >
    > <form onsubmit="return myHandler(this);">


    I did try this but it didn't work - probably because I didn't do it
    properly (left the form.submit in there etc etc). I'll have to do it
    more carefully and see how I get on.

    I would have preferred my solution if I could have made that disable the
    effect of return doing a submit - but anyway.

    Funnily enough, from what I read in the JavaScript Bible I was left with
    the impression that the return/enter key *should* do a submit - but
    Safari seems the only one that does that.

    Thanks,
    Tim Streater, Jun 12, 2007
    #2
    1. Advertising

  3. Tim Streater

    Randy Webb Guest

    Good Man said the following on 6/12/2007 11:56 AM:
    > Tim Streater <> wrote in
    > news::
    >
    >> I have a form and a button to submit it. The button is made from:
    >>
    >> <input type=button onclick='myHandler(this.form);'>
    >>
    >> This all works fine except that in Safari 2.0.4, the enter/return
    >> keys, if pressed, submit the form - bypassing my onclick handler.
    >>
    >> I can partially fix this with:
    >>
    >> <form onsubmit='return false;'>

    >
    > First off, though this isn't the reason for your problem, you can't expect
    > javascript to work properly if your (X)HTML markup is bad.


    There is nothing "bad" or "invalid" about that HTML. The only time
    attributes are *required* to be quoted is if they have a space or other
    special characters in them.

    > Your button script should be <input type="button" onclick="myHandler(this.form);">
    > (quotes around the input type).


    Nonsense. The code posted validates just fine.

    > You will no doubt find javascript behaving oddly if your document isn't
    > valid.


    Now that is true but it isn't the problem here as it is irrelevant.

    > That aside, why can't you put the myHandler function on the form submit?


    Because the form submit even handler probably doesn't do what is wanted.
    The only way that would get fired is to submit the form and that isn't
    always desired. It would be pretty insane if myHandler simply set the
    value of an input.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 12, 2007
    #3
  4. Tim Streater

    Randy Webb Guest

    Tim Streater said the following on 6/12/2007 12:26 PM:
    > In article <Xns994D797BF87D0sonicyouth@216.196.97.131>,
    > Good Man <> wrote:
    >
    >> Tim Streater <> wrote in
    >> news::
    >>
    >>> I have a form and a button to submit it. The button is made from:
    >>>
    >>> <input type=button onclick='myHandler(this.form);'>
    >>>
    >>> This all works fine except that in Safari 2.0.4, the enter/return
    >>> keys, if pressed, submit the form - bypassing my onclick handler.
    >>>
    >>> I can partially fix this with:
    >>>
    >>> <form onsubmit='return false;'>

    >> First off, though this isn't the reason for your problem, you can't expect
    >> javascript to work properly if your (X)HTML markup is bad. Your button
    >> script should be <input type="button" onclick="myHandler(this.form);">
    >> (quotes around the input type).

    >
    > Are you saying that these:
    >
    > <form onsubmit='return false;'>
    > <form onsubmit="return false;">
    >
    > are not equivalent?


    No, he was attempting to say that type=button was invalid HTML, which it
    isn't, and that you should have quoted it type="button" which is totally
    irrelevant as either is valid HTML.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 12, 2007
    #4
  5. Tim Streater

    Tim Streater Guest

    In article <>,
    Randy Webb <> wrote:

    > Good Man said the following on 6/12/2007 11:56 AM:
    > > Tim Streater <> wrote in
    > > news::
    > >
    > >> I have a form and a button to submit it. The button is made from:
    > >>
    > >> <input type=button onclick='myHandler(this.form);'>
    > >>
    > >> This all works fine except that in Safari 2.0.4, the enter/return
    > >> keys, if pressed, submit the form - bypassing my onclick handler.
    > >>
    > >> I can partially fix this with:
    > >>
    > >> <form onsubmit='return false;'>

    > >
    > > First off, though this isn't the reason for your problem, you can't expect
    > > javascript to work properly if your (X)HTML markup is bad.

    >
    > There is nothing "bad" or "invalid" about that HTML. The only time
    > attributes are *required* to be quoted is if they have a space or other
    > special characters in them.
    >
    > > Your button script should be <input type="button"
    > > onclick="myHandler(this.form);">
    > > (quotes around the input type).

    >
    > Nonsense. The code posted validates just fine.
    >
    > > You will no doubt find javascript behaving oddly if your document isn't
    > > valid.

    >
    > Now that is true but it isn't the problem here as it is irrelevant.
    >
    > > That aside, why can't you put the myHandler function on the form submit?

    >
    > Because the form submit even handler probably doesn't do what is wanted.
    > The only way that would get fired is to submit the form and that isn't
    > always desired. It would be pretty insane if myHandler simply set the
    > value of an input.


    myHandler in fact delves into a number of iframes to pull some values
    out and stick them in the form. Hence the disaster of having it
    bypassed. You can hit return quite easily if you're aiming for backspace.
    Tim Streater, Jun 12, 2007
    #5
  6. Tim Streater

    Tim Streater Guest

    In article <>,
    Tim Streater <> wrote:

    > I have a form and a button to submit it. The button is made from:
    >
    > <input type=button onclick='myHandler(this.form);'>
    >
    > This all works fine except that in Safari 2.0.4, the enter/return keys,
    > if pressed, submit the form - bypassing my onclick handler.
    >
    > I can partially fix this with:
    >
    > <form onsubmit='return false;'>
    >
    > except that if at the time enter/return is pressed, the focus is on one
    > of the iframes in my form, then the iframe gets reloaded (with goofy
    > parameters, of course). If the focus is nowhere when enter/return is
    > pressed, then these get ignored OK.


    I added the <form onsubmit='return false;'> also to the <form>s in my
    iframes, and now all is well.
    Tim Streater, Jun 13, 2007
    #6
  7. Tim Streater

    Randy Webb Guest

    Good Man said the following on 6/12/2007 1:20 PM:
    > Randy Webb <> wrote in
    > news::
    >
    >
    >>> Are you saying that these:
    >>>
    >>> <form onsubmit='return false;'>
    >>> <form onsubmit="return false;">
    >>>
    >>> are not equivalent?

    >> No, he was attempting to say that type=button was invalid HTML, which
    >> it isn't, and that you should have quoted it type="button" which is
    >> totally irrelevant as either is valid HTML.

    >
    > Sure, while <input type=button value="dance" /> is valid HTML


    Only if you want the consequences, or possible consequences, of the / in
    the end of it which gives the browser all rights to display the > on the
    page.

    > , it is not > valid XHTML, which I should have specified


    You specified (X)HTML which implies both. It is valid HTML and anybody
    that serves XHTML, on the web, doesn't need a browser, they need a
    straight jacket and a competent doctor to treat them.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 13, 2007
    #7
  8. Tim Streater

    Randy Webb Guest

    Good Man said the following on 6/13/2007 1:08 PM:
    > Randy Webb <> wrote in news:Rv-
    > :
    >
    >
    >> You specified (X)HTML which implies both. It is valid HTML and anybody
    >> that serves XHTML, on the web, doesn't need a browser, they need a
    >> straight jacket and a competent doctor to treat them.

    >
    > Thanks for the opinion, but I won't get sucked into that debate at the
    > moment.


    Then don't use XHTML as a defense for an incorrect statement :)

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jun 13, 2007
    #8
    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. MB
    Replies:
    3
    Views:
    8,694
    =?Utf-8?B?c3Jpbmk=?=
    May 13, 2004
  2. Replies:
    1
    Views:
    1,419
  3. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    375
    Anders Matthiessen
    Jan 12, 2005
  4. Matt
    Replies:
    1
    Views:
    496
    Michael Winter
    Mar 6, 2004
  5. HopfZ
    Replies:
    0
    Views:
    542
    HopfZ
    Aug 28, 2006
Loading...

Share This Page