Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just tested this behavior in different browsers, all browsers gave same results, here's my conclusion:</p> <pre><code>&lt;!-- submit button inside form --&gt; &lt;form method="post"&gt; &lt;input type="submit" value="submit" /&gt; &lt;!-- onclick = submitted to same page --&gt; &lt;input type="submit" value="submitNredirect" onclick="document.location.href='http://www.google.com';" /&gt; &lt;!-- onclick = submitted to same page, ignored redirect --&gt; &lt;/form&gt; &lt;!-- submit button without form --&gt; &lt;input type="submit" value="submit" /&gt; &lt;!-- onclick = no submit or redirect occur --&gt; &lt;input type="submit" value="submitNredirect" onclick="document.location.href='http://www.google.com';" /&gt; &lt;!-- onclick = redirect --&gt; </code></pre> <p>As you know, javascript is linear, it executes the line of code that comes first. Let's say we have this form:</p> <pre><code>&lt;form id="myform" method="post" onsubmit="alert('onsubmit');"&gt; &lt;input id="mybtn" type="submit" value="submit" onclick="alert('onclick');" /&gt; &lt;/form&gt; &lt;script&gt; $(document).ready(function() { $('#mybtn').bind('click', function() { alert('bind click'); }); $('#myform').bind('submit', function() { alert('bind submit'); }); // onclick &gt; bind click &gt; onsubmit &gt; bind submit }); &lt;/script&gt; </code></pre> <p>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:</p> <ol> <li>alert: onclick</li> <li>alert: bind click</li> <li>alert: onsubmit</li> <li>alert: bind submit</li> <li>form submission</li> </ol> <p>If you include any redirect code in any of the functions, redirect will be ignored and submission occurs, I believe javascript overwrites the <code>document.location.href</code> 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 <code>return false;</code> at some point, this implicit/hidden line is executed.</p> <p>I'm not an expert, but that's what I understood after some testing. Please correct me if I'm wrong.</p> <p>EDIT: What if we include a <code>document.location.href</code> in each function? like this:</p> <pre><code>&lt;form id="myform" method="post" onsubmit="document.location.href='http://www.onsumbit.com';"&gt; &lt;input id="mybtn" type="submit" value="submit" onclick="document.location.href='http://www.onclick.com';" /&gt; &lt;/form&gt; &lt;script&gt; $(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'; }); }); &lt;/script&gt; </code></pre> <p>The state of the string value of "href" changes as follows (but executes the redirect at the end of script):</p> <ol> <li>document.location.href = '<a href="http://www.onclick.com" rel="nofollow">http://www.onclick.com</a>';</li> <li>document.location.href = '<a href="http://www.bindclick.com" rel="nofollow">http://www.bindclick.com</a>';</li> <li>document.location.href = '<a href="http://www.onsubmit.com" rel="nofollow">http://www.onsubmit.com</a>';</li> <li>document.location.href = '<a href="http://www.bindsubmit.com" rel="nofollow">http://www.bindsubmit.com</a>';</li> <li>document.location.href = '/'; // form submission</li> <li>javascript, do redirect based on "href" string value</li> </ol> <p>but we have <code>return false;</code> after #2, which means will do redirect for "<a href="http://www.bindclick.com" rel="nofollow">http://www.bindclick.com</a>"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload