Safari enter/return bypassing my button script

T

Tim Streater

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,
 
T

Tim Streater

Good Man said:
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,
 
R

Randy Webb

Good Man said the following on 6/12/2007 11:56 AM:
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.
 
R

Randy Webb

Tim Streater said the following on 6/12/2007 12:26 PM:
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.
 
T

Tim Streater

Randy Webb said:
Good Man said the following on 6/12/2007 11:56 AM:

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.


Nonsense. The code posted validates just fine.


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


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.
 
T

Tim Streater

Tim Streater said:
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.
 
R

Randy Webb

Good Man said the following on 6/12/2007 1:20 PM:
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.
 
R

Randy Webb

Good Man said the following on 6/13/2007 1:08 PM:
(e-mail address removed):



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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top