onSubmit ALWAYS returns true when it sets location.href?

O

OtisUsenet

Hello,

I am trying to call Javascript from FORM's onSubmit, and return false,
so the form is not actually submitted.
Normally I can just add 'return false;' to onSubmit to accomplish
this, like so:

<form name="XX" method="get" action="/Foo" onsubmit="onSearch();
return false;">

However, if my Javascript sets 'location.href', then my 'return
false;' is never executed:

<script type="text/javascript" language="JavaScript">
<!--
function onSearch() {
location.href = "/Bar?q=" +
encode(document.forms["XX"].elements["q"].value);
}
// -->
</script>


Is this a known behaviour? Are there any workarounds?

What is happening here is that instead of location.href getting set to
/Bar?q=..... and the browser going to that URL, my form's /Foo action
gets called, and that is what I am trying to avoid. Any ideas how I
can avoid /Foo getting called?

Thanks!
 
R

Randy Webb

OtisUsenet said:
Hello,

I am trying to call Javascript from FORM's onSubmit, and return false,
so the form is not actually submitted.

Then why not just use a button?

<input type="button" value="Something" onclick="onSearch()">

<form onsubmit="return false">

Now, the form won't get submitted at all if scripting is enabled.

<--snip-->
 
T

Thomas 'PointedEars' Lahn

OtisUsenet said:
I am trying to call Javascript from FORM's onSubmit, and return false,
so the form is not actually submitted.
Normally I can just add 'return false;' to onSubmit to accomplish
this, [...]
However, if my Javascript sets 'location.href', then my 'return
false;' is never executed:

<script type="text/javascript" language="JavaScript">

language="JavaScript" is not needed anymore. With
(X)HTML Strict and XHTML 1.1 it is disallowed.

Commenting out scripts is an obsoleted technique since there
are no working UAs out there that require it. HTML 2.0 has
been obsoleted and HTML 3.2 defines the "script" element.
function onSearch() {
location.href = "/Bar?q=" +
encode(document.forms["XX"].elements["q"].value);
}

What is this encode() method? It is not a built-in.
Are you sure you are not getting script errors? If no,
that would explain why "return false" is not executed.

If document.forms["XX"] is the form you try to submit,
pass a `this' reference to the method in its onsubmit
handler and use the method's argument instead.

Remove that, too.
</script>


Is this a known behaviour?

Yes, changing the current container element's resource should result
in all information of the contents (document) to be lost.
Are there any workarounds?

Don't do it.
What is happening here is that instead of location.href getting set to
/Bar?q=..... and the browser going to that URL, my form's /Foo action
gets called, and that is what I am trying to avoid.

Unless there are script errors, I would consider *that* a bug. Once the
location changes, the other resource should be accessed and no code of the
previous resource should be executed.
Any ideas how I can avoid /Foo getting called?

If there are no script errors, use either working UA or no onsubmit handler.
I wonder why you even change `location' on submit.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top