javascript - Why submitting a form overrides setting location.href in a submit button click handler? -
is inspired by this question.
In short: why window.location.href
is not being redirected to a new page (example.com) while executing the code given below?
& lt; Form & gt; & Lt; Input type = "submit" id = "submit" value = "submit" & gt; & Lt; / Form & gt; & Lt; Script & gt; Document.getElementById ('Submit'). AddEventListener ('click', function (e) {window.location.href = "http://www.example.com";}); & Lt; / Script & gt;
I always believe that the window.location.href
setting immediately loads a new page, but in this situation it is not. Rather than submitting a form the page is reloaded, and setting a new location seems completely undisclosed. Why? how? What am I missing here?
Please note, that I am aware of many ways to stop the way I present this method, but I want to know why Additional information This is happening in all major browsers (Chrome, FF, IE 11), but when no code is run in a stack snippet, the function shows a < Code> alert / console.log location.href setting
is inserted, that click submit
before handler click Is executed.
A problem.
If you try to submit the form at a faster pace as a replacement location,
If you check your browser network tab then you can see that the submission request was canceled (but still sent) to the request Redirect. I believe the same situation occurs when you are trying to do it on onclick
or onsubmit
, the first request is to cancel the next one and Window.location.href
redirection
Comments
Post a Comment