在javascript重定向(设置window.location.href)后代码会发生什么? [英] What happens to code after a javascript redirect (setting window.location.href)?

查看:3736
本文介绍了在javascript重定向(设置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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆