Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you try to redirect before a form submit?

I came across this strange code in a website I'm working on and don't understand how or why the behavior I'm seeing is happening.

<input type="submit" value="Save" onclick="document.location.href='http://www.google.com'" />

(Changed the href; it was actually pointing to the same URL as the form's action).

Because the onclick event fires before the form is submitted, I would expect this code to redirect the page to Google.com, and thus stop executing the page and never submit the form. Or possibly redirect to Google.com but then still finish submitting the form.

But what happens seems to be that the redirect is just completely ignored, and the form gets submitted anyway. Why would this happen?
I confirmed that the onclick event fires just fine; I moved the redirect into a function and called the function in onclick. The function was called just fine before the submit, but the redirect seemed to be ignored.

like image 616
GendoIkari Avatar asked Aug 16 '13 20:08

GendoIkari


1 Answers

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:

  1. alert: onclick
  2. alert: bind click
  3. alert: onsubmit
  4. alert: bind submit
  5. 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):

  1. document.location.href = 'http://www.onclick.com';
  2. document.location.href = 'http://www.bindclick.com';
  3. document.location.href = 'http://www.onsubmit.com';
  4. document.location.href = 'http://www.bindsubmit.com';
  5. document.location.href = '/'; // form submission
  6. 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"

like image 163
evilReiko Avatar answered Oct 04 '22 21:10

evilReiko