473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

onSubmit ALWAYS returns true when it sets location.href?

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!
Jul 23 '05 #1
2 3264
OtisUsenet wrote:
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-->

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
OtisUsenet wrote:
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
--
"You have to mount it before you fsck it."
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
3
by: n3mtkj3mzsir0cy | last post by:
I have created web pages that do client-side form validation using the onclick directive. E.g., <form action=other_page.cgi method=post> Enter your age: <input name=age> <input type="submit"...
2
by: Sean Dockery | last post by:
Which is the following is correct? a) <form ... onSubmit="return checkData()"> b) <form ... onSubmit="return checkData();"> c) <form ... onSubmit="checkData()"> d) <form ......
8
by: Chris Kettenbach | last post by:
I have a function that validates a text box. Is there anyway to run that function on submit and if it returns false to cancel the submit. Any ideas. Thanks Guys! This group is awesome. Thanks,...
4
by: usl2222 | last post by:
Hi folks, I appreciate any assistance in the following problem: I have a form with a bunch of dynamic controls on it. All the controls are dynamically generated on a server, including all...
8
by: M.L. | last post by:
Hello. I created a form using JS validation with the form tag as follows: <form name="form1" action="dynaform.php" method="post" onsubmit="return pvg_sub();"> The js validation script sends...
1
by: vunet | last post by:
I write a JS library component which is applied to every form on a webpage. The component does something before it submits the form. Now, let's say user has his own onSubmit() handlers returning...
1
by: pechar | last post by:
Hi all, I'm absolutely lost and have no idea why this is happening. I have a master page where the form resides. I set an onsubmit attribute to call a method which sets a variable to true ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.