javascript 重定向(设置 window.location.href)后代码会发生什么? [英] What happens to code after a javascript redirect (setting window.location.href)?
问题描述
我有以下 javascript 重定向代码和更多代码.
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.
在 firefox 中,第一个警报在被重定向之前的一瞬间是可见的.第二个警报根本不可见.
In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.
在 chrome 中,两个警报都会弹出,并且只有在两个警报都单击确定后才会发生重定向.
In chrome, both alerts will pop up, and the redirect happens only after clicking ok for both alerts.
是否有一些可以解决这些差异的重定向后代码会发生什么的概念?如果重定向是异步的,那么 chrome 在做什么?我想了解正在发生的事情,以便我可以确定在更复杂的情况下该怎么做,在这种情况下,重定向深埋在某些回调和其他逻辑中.
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.
推荐答案
浏览器会尝试执行 window.location.href = 'url'
之后的代码,直到页面跳转到下一个网页地址,因此将执行的代码行数取决于浏览器的速度
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
这篇关于javascript 重定向(设置 window.location.href)后代码会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!