What happens to code after a javascript redirect (setting window.location.href)?

谁说我不能喝 提交于 2019-11-26 16:40:02

问题


I have the following javascript redirect code followed by some more code.

window.location.href = '/someurl';
alert('hello');
alert('hello again');

This causes a browser inconsistency.

In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.

In chrome, both alerts will pop up, and the redirect happens only after clicking ok for both alerts.

Is there some concept of what happens to code after the redirect that can resolve these differences? If the redirect is asynchronous, then what is chrome doing? I would like to understand what is happening so that I can determine what to do in a more complicated scenario where a redirect is buried deep within some callbacks and other logic.


回答1:


The browser will try to execute the code after window.location.href = 'url' until the page goes to the next web adress, so the number of lines of code that will be executed depends on the browser's speed




回答2:


The Javascript session will struggle to continue it's mission in the face of impending doom. Coding in this manner is considered unpredictable. ...and cruel.



来源:https://stackoverflow.com/questions/10525584/what-happens-to-code-after-a-javascript-redirect-setting-window-location-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!