location是否会停止执行javascript

location是否会停止执行javascript

本文介绍了更改window.location是否会停止执行javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写服务器端代码时,需要在向客户端发送Location:...标题后明确停止执行,否则您的代码将继续在后台执行。

When writing server-side code you need to explicitly stop execution after sending a "Location: ..." header to the client or your code will continue to execute in the background.

但是当你在客户端脚本中更改window.location时呢?这是否会立即停止执行当前脚本,还是由程序员来确保未达到此调用后的任何代码?

But what about when you change window.location in a client-side script? Does this immediately stop execution of the current script or is it up to the programmer to make sure that any code located after this call is not reached?

推荐答案

设置 window.location 不会隐式停止JS执行。以下面的例子为例:

Setting window.location does not implicitly stop JS execution. Take the following as an example:

function locationTest() {
  window.location = 'http://www.google.com/';
  window.open('http://www.yahoo.com/');
}

locationTest();

尝试从Firebug / Web Inspector /等运行。你会注意到当前窗口会加载谷歌,但雅虎也会打开一个新窗口。

Try running that from Firebug/Web Inspector/etc. and you'll notice that the current window will load Google, but a new window will open with Yahoo as well.

这篇关于更改window.location是否会停止执行javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:03