By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

Safari enter/return bypassing my button script

P: n/a
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,
Jun 12 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Tim Streater <ti**********@dante.org.ukwrote in
news:ti********************************@news.indiv idual.net:
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).

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

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);">
Jun 12 '07 #2

P: n/a
Good Man said the following on 6/12/2007 11:56 AM:
Tim Streater <ti**********@dante.org.ukwrote in
news:ti********************************@news.indiv idual.net:
>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/
Jun 12 '07 #3

P: n/a
In article <Xn************************@216.196.97.131>,
Good Man <he***@letsgo.comwrote:
Tim Streater <ti**********@dante.org.ukwrote in
news:ti********************************@news.indiv idual.net:
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,
Jun 12 '07 #4

P: n/a
Tim Streater said the following on 6/12/2007 12:26 PM:
In article <Xn************************@216.196.97.131>,
Good Man <he***@letsgo.comwrote:
>Tim Streater <ti**********@dante.org.ukwrote in
news:ti********************************@news.indi vidual.net:
>>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/
Jun 12 '07 #5

P: n/a
In article <Tt*********************@telcove.net>,
Randy Webb <Hi************@aol.comwrote:
Good Man said the following on 6/12/2007 11:56 AM:
Tim Streater <ti**********@dante.org.ukwrote in
news:ti********************************@news.indiv idual.net:
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.
Jun 12 '07 #6

P: n/a
Randy Webb <Hi************@aol.comwrote in
news:3d********************@telcove.net:

>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, it is not
valid XHTML, which I should have specified

validator.w3.org/check:

**

This page is not Valid XHTML 1.0 Transitional!

Below are the results of checking this document for XML well-formedness
and validity.

1. Error Line 9 column 12: an attribute value specification must be
an attribute value literal unless SHORTTAG YES is specified.

<input type=button value="dance" />
Jun 12 '07 #7

P: n/a
In article <ti********************************@news.individua l.net>,
Tim Streater <ti**********@dante.org.ukwrote:
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.
Jun 13 '07 #8

P: n/a
Good Man said the following on 6/12/2007 1:20 PM:
Randy Webb <Hi************@aol.comwrote in
news:3d********************@telcove.net:

>>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/
Jun 13 '07 #9

P: n/a
Randy Webb <Hi************@aol.comwrote in news:Rv-
dn*****************@telcove.net:

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.
Jun 13 '07 #10

P: n/a
Good Man said the following on 6/13/2007 1:08 PM:
Randy Webb <Hi************@aol.comwrote in news:Rv-
dn*****************@telcove.net:

>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/
Jun 13 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.