什么时候是window.location =" url"执行? [英] When is window.location = "url" executed?

查看:81
本文介绍了什么时候是window.location =" url"执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


示例代码如下(

开头的括号中的数字仅供参考)(完整的HTML文件是最后

(本文):


(1)window.location =''http://www.google.com'';

(2)alert(我还在这里!);

(3)window.open(" http://www.slashdot。 com"," _blank");


会发生什么,是在赋值后(1)理论上其余的

代码不应该执行,因为窗口应该加载一个

的新URL。但是在我的浏览器(GNU / Linux上的Firefox 1.0)中,浏览器也会执行(2)和(3)。

似乎窗口只加载了

脚本完成执行后的新内容。

这种行为是错误还是正确?


在我搜索的所有文件中没有提及何时应该加载新的

位置,如果立即或仅在脚本

完成执行后。


任何人都可以尝试使用IE吗?


谢谢


罗兰


完整档案:


< html:html>

< head>

< script type =" text / javascript"> ;

window.location =''http://www.google.com'';

alert(我还在这里!);

window.open(" http://www.slashdot.com"," _blank");

< / script>

< / head>

< body>

测试。

< / body>

< / html:html>

Hello,

the example code is the following(the number in parentheses at the
beginning are just for reference)(The complete HTML file is at the end
of this article):

(1)window.location = ''http://www.google.com'';
(2)alert("I''m still here!");
(3)window.open("http://www.slashdot.com", "_blank");

What happens, is that after the assignment (1) theoretically the rest
of the code shouldn''t execute, since the window should be loading a
new URL. But in my browser(Firefox 1.0 on GNU/Linux) the browser also
executes (2) and (3).
It seems that the window is only loaded with the new content after the
script has finished executing.
So is this behaviour wrong or correct?

In all documentations I searched there is no mention of WHEN the new
location should be loaded, if immediately or only after the script
finished executing.

Can anyone try this with IE?

Thank you

Roland

Complete file:

<html:html>
<head>
<script type="text/javascript">
window.location = ''http://www.google.com'';
alert("I''m still here!");
window.open("http://www.slashdot.com", "_blank");
</script>
</head>
<body>
Test.
</body>
</html:html>

推荐答案

Roland写道:

< snip>
Roland wrote:
<snip>
(1)window.location =''http://www.google.com'';
( 2)警报(我还在这里!);
(3)window.open(" http://www.slashdot.com"," _blank");

会发生什么,在赋值(1)理论上后,其余的代码不应该执行,因为窗口
应该加载一个新的URL。


如果您的期望是位置属性的分配是

最后一次操作那么为什么会有代码呢?

但不是,对location属性的赋值不会立即替换

页面,它可能(可能确实)立即发出HTTP请求

,最终可能会返回一个响应,该结果将导致页面被替换为
。但是那将是一个异步过程

,所以没有理由期望脚本终止于作业的

点。

但是在我的浏览器(GNU / Linux上的Firefox 1.0)中,浏览器也执行(2)和(3)。
似乎窗口只加载了新的内容
之后脚本已完成执行。


最有可能的页面只有当它的替换来自服务器的

时才会被替换。

因此这种行为是错误的或正确的?


没有规范定义分配给该位置的行为

对象可以预期任何单一行为。

In我搜索过的所有文档都没有提及
何时应该加载新位置,如果是立即
或仅在脚本完成执行后。


期望在对HTTP请求的

响应之前替换页面是不现实的,没有理由期待HTTP

请求/响应过程限制或限制执行

脚本。

任何人都可以尝试使用IE吗?
(1)window.location = ''http://www.google.com'';
(2)alert("I''m still here!");
(3)window.open("http://www.slashdot.com", "_blank");

What happens, is that after the assignment (1) theoretically
the rest of the code shouldn''t execute, since the window
should be loading a new URL.
If your expectation is that the assignment to the location property is
the last operation then why is there code after it?

But no, the assignment to the location property does not replace the
page immediately, it might (probably does) make an HTTP request
immediately, that may ultimately return a response that will result in
the page being replaced. But that would be an asynchronous process
anyway so there is not reason to expect the script to terminate at the
point of the assignment.
But in my browser(Firefox 1.0 on GNU/Linux) the browser
also executes (2) and (3).
It seems that the window is only loaded with the new content
after the script has finished executing.
Most likely the page is only replaced when its replacement arrives from
the server.
So is this behaviour wrong or correct?
No specification defines behaviour for assignments to the location
object to the extent that any single behaviour could be expected.
In all documentations I searched there is no mention of
WHEN the new location should be loaded, if immediately
or only after the script finished executing.
It would be unrealistic to expect the page to be replaced prior to the
response to the HTTP request, and no reason to expect the HTTP
request/response process to confine, or be confined by, the execution of
scripts.
Can anyone try this with IE?



< snip>


到底是什么?如果您不希望脚本在分配到

之后执行,那么位置属性就不会在分配后放置代码。


Richard。


<snip>

To what end? If you don''t want script to execute after the assignment to
the location property then don''t put code after the assignment.

Richard.


你应该用一个BODY ONLOAD做一个IF,但是一个简单的EXIT,如下面的
所示,应该回答这个问题。 />

< html>

< head>

< script type =" text / javascript">

window.location =''http://www.askblax.com'';

exit(); //离开舞台

提醒(永远不要联系我!);

window.open(" http://www.google.com", " _blank");


< / script>

< / head>

< body bgcolor = pink> ;

之前没有有效的测试,我不会重定向页面这个

方式。< BR>< BR>如果没有有效的测试,我将不会重定向页面这个

的方式。达到此行的唯一方法是在退出命令之前发生javascript错误



< / body>

< / html>

http://www.askblax.com

You should do an IF with a BODY ONLOAD, but a simple EXIT, as shown
below, ought to answer the question.

<html>
<head>
<script type="text/javascript">
window.location = ''http://www.askblax.com'';
exit(); // stage left
alert("Never gonna reach me!");
window.open("http://www.google.com", "_blank");

</script>
</head>
<body bgcolor=pink>
Without a valid test before, I will not redirect pages this
way.<BR><BR>Without a valid test before, I will not redirect pages this
way. The only way to reach this line is if a javascript error occurs
before the exit command.
</body>
</html>

http://www.askblax.com


>>最有可能的是页面只有在更换到达时才会被替换

来自服务器的
。 <


我认为你是对的。
>> Most likely the page is only replaced when its replacement arrives
from
the server. <<

I think you are right.
到底是什么?如果你不希望脚本执行
To what end? If you don''t want script to execute after the



赋值给

的位置属性那么就不要把代码放在分配。 <


帧可能。


assignment to
the location property then don''t put code after the assignment. <<

Frames maybe.


这篇关于什么时候是window.location =&quot; url&quot;执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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