Why submitting a form overrides setting location.href in a submit button click handler?

前端 未结 3 886
Happy的楠姐
Happy的楠姐 2020-12-20 03:31

This question is inspired by this post.

In a nutshell: Why window.location.href is not redirecting to a new page (example.com) when executing the code be

3条回答
  •  感动是毒
    2020-12-20 03:49

    You can see easier here what is happening step by step if you will try tu change location drunning form submission

    JSFIDDLE

    If you will check your browser network tab than you can see that the submit request is cancelled (but still sent) by redirect request. I believe that same situation occurs when you trying to do it onclick or onsubmit the first request just cancelling the next one and prevent window.location.href redirection.

    enter image description here

提交回复
热议问题