2

I tested the following javascript code on a webpage.

<form method=POST id="someform" action="http://localhost/somepage.php" >
<input type=hidden name=stuff value="value">
</form>
<script>
document.forms['someform'].submit();
window.location="http://google.com";
</script>

The window successfully redirects to google after the form is submitted. I don't understand why. Isn't the control transfered to somepage.php? why is the window.location ... still executed.

asked May 9, 2011 at 23:32
2
  • How do you know that it submits? That's what my code looks like and the form doesn't submit. I entered incorrect information into the credit card fields and it should error upon submission, but it redirects to the page I have window.location pointing to. Commented Oct 12, 2012 at 16:31
  • If you check your debug tools, you "probably" will see that the browser has made two request. One POST or GET (using the form's functionality) and one GET (the "redirect" using javascript). I really don't know how the browser decides what request to serve. Commented Aug 14, 2014 at 20:38

1 Answer 1

2

Everything that is called is executed while the page doesn't disappear. The submit() call doesn't block the execution, it returns immediately.

answered May 9, 2011 at 23:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.