JavaScript重定向后代码会发生什么?
我有以下JavaScript重定向代码,然后再进行一些代码。
window.location.href = '/someurl';
alert('hello');
alert('hello again');
这会导致浏览器不一致。
在Firefox中,在重定向之前,可以看到第一个警报的右第二警报。第二个警报根本不可见。
在Chrome中,两个警报都会弹出,并且重定向仅在单击两个警报后才发生。
是否有一些可以解决这些差异的重定向后代码会发生什么概念?如果重定向是异步的,那么Chrome在做什么?我想了解正在发生的事情,以便在更复杂的场景中确定如何在某些回调和其他逻辑中埋葬在更复杂的情况下。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
面对即将来临的厄运,JavaScript会议将努力继续其使命。以这种方式编码被认为是不可预测的。 ...残酷。
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.
浏览器将尝试在
window.location.href ='url'
之后执行代码,直到页面进入下一个Web Adress,因此将执行的代码行数取决于浏览器的。速度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