I've just tested this behavior in different browsers, all browsers gave same results, here's my conclusion:
<!-- submit button inside form -->
<form method="post">
<input type="submit" value="submit" /> <!-- onclick = submitted to same page -->
<input type="submit" value="submitNredirect" onclick="document.location.href='http://www.google.com';" /> <!-- onclick = submitted to same page, ignored redirect -->
</form>
<!-- submit button without form -->
<input type="submit" value="submit" /> <!-- onclick = no submit or redirect occur -->
<input type="submit" value="submitNredirect" onclick="document.location.href='http://www.google.com';" /> <!-- onclick = redirect -->
As you know, javascript is linear, it executes the line of code that comes first. Let's say we have this form:
<form id="myform" method="post" onsubmit="alert('onsubmit');">
<input id="mybtn" type="submit" value="submit" onclick="alert('onclick');" />
</form>
<script>
$(document).ready(function() {
$('#mybtn').bind('click', function() {
alert('bind click');
});
$('#myform').bind('submit', function() {
alert('bind submit');
});
// onclick > bind click > onsubmit > bind submit
});
</script>
On clicking the button, the events bound to the button occurs first (the "click" events), then the events bound to the form occurs next (the "submit" events).
The result is as follow:
- alert: onclick
- alert: bind click
- alert: onsubmit
- alert: bind submit
- form submission
If you include any redirect code in any of the functions, redirect will be ignored and submission occurs, I believe javascript overwrites the document.location.href
with the form post process, the "form submission" is like an implicit/hidden line of code at the end of the script, if you don't return false;
at some point, this implicit/hidden line is executed.
I'm not an expert, but that's what I understood after some testing. Please correct me if I'm wrong.
EDIT: What if we include a document.location.href
in each function? like this:
<form id="myform" method="post" onsubmit="document.location.href='http://www.onsumbit.com';">
<input id="mybtn" type="submit" value="submit" onclick="document.location.href='http://www.onclick.com';" />
</form>
<script>
$(document).ready(function() {
$('#mybtn').bind('click', function() {
document.location.href='http://www.bindclick.com';
return false;
});
$('#myform').bind('submit', function() {
document.location.href='http://www.bindsumbit.com';
});
});
</script>
The state of the string value of "href" changes as follows (but executes the redirect at the end of script):
- document.location.href = 'http://www.onclick.com';
- document.location.href = 'http://www.bindclick.com';
- document.location.href = 'http://www.onsubmit.com';
- document.location.href = 'http://www.bindsubmit.com';
- document.location.href = '/'; // form submission
- javascript, do redirect based on "href" string value
but we have return false;
after #2, which means will do redirect for "http://www.bindclick.com"
someform.submit()
at the end of the button's action-eventcode. Alternatively, you may override theonsubmit
form-attribute or respective submit-eventhandler.