JavaScript重定向后代码会发生什么?

发布于 2025-02-13 03:40:35 字数 362 浏览 3 评论 0原文

我有以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2025-02-20 03:40:35

面对即将来临的厄运,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.

寂寞美少年 2025-02-20 03:40:35

浏览器将尝试在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文