文档头中的脚本何时执行? [英] When do scripts in document head execute?

查看:45
本文介绍了文档头中的脚本何时执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的文件:


< html>

< head>

< script language = javascript>

window.location.href =''file.php'';

< / script>

< / head>

< body>

正文内容

< / body>

< / html>


头部的JavaScript会在正文加载之前执行,还是会在新页面加载之前看到内容的短暂闪现?


If I have a document like this:

<html>
<head>
<script language=javascript>
window.location.href=''file.php'';
</script>
</head>
<body>
body content
</body>
</html>

will the JavaScript in the head execute before the body loads, or will
the visitor see a short flash of the content before the new page loads?

推荐答案

" Manfred Kooistra" < ma ************** @ gmx.dewrote in message

news:11 **************** ****** @6g2000cwy.googlegrou ps.com ...
"Manfred Kooistra" <ma**************@gmx.dewrote in message
news:11**********************@6g2000cwy.googlegrou ps.com...

如果我有这样的文件:


< html>

< head>

< script language = javascript>

window.location.href =''file .php'';

< / script>

< / head>

< body>

正文内容

< / body>

< / html>


头部的JavaScript会在之前执行在加载新页面之前,访问者会看到内容的短暂闪现,或者是否需要

If I have a document like this:

<html>
<head>
<script language=javascript>
window.location.href=''file.php'';
</script>
</head>
<body>
body content
</body>
</html>

will the JavaScript in the head execute before the body loads, or will
the visitor see a short flash of the content before the new page loads?



1)你试过吗?


2)你想要什么行为?

如果你想立即去另一个页面,你可以这样做:


< html>

< head>

< title> 1.htm< / title>

< / head>

< body onload =" window.location.href =''file.php'';">

正文内容

< / body>

< / html>


或者这个:


< html>

< head>

< title> 2.htm< / title>

< script type =" text / javascript">

window.onload = function(){

window.location.href =''file.php'';

}

< / script>

< / head>

< body>

身体内容

< / body>

< / html>


甚至这个(这不需要启用JavaScript):


< html>

< head>

< title> 3.htm< / title>

< meta http-equiv =" refresh" content =" 0;

URL = http:// {your-domain} /file.php'';">

< / head>

< body>

正文内容

< / body>

< / html>


但你为什么要这样?

1) Did you try it?

2) What behavior do you want?

If you want to go to another page immediately, you can do this:

<html>
<head>
<title>1.htm</title>
</head>
<body onload="window.location.href=''file.php'';">
body content
</body>
</html>

Or this:

<html>
<head>
<title>2.htm</title>
<script type="text/javascript">
window.onload = function () {
window.location.href=''file.php'';
}
</script>
</head>
<body>
body content
</body>
</html>

Or even this (which doesn''t require that JavaScript be enabled):

<html>
<head>
<title>3.htm</title>
<meta http-equiv="refresh" content="0;
URL=http://{your-domain}/file.php'';">
</head>
<body>
body content
</body>
</html>

But why would you?




Manfred Kooistra写道:

Manfred Kooistra wrote:

如果我有这样的文件:


< html>

< head>

< script language = javascript>

window.location.href =''file.php'';

< / script>

< / head>

< body>

身体内容

< / body>

< ; / html>


将在主体加载之前执行头部的JavaScript,
If I have a document like this:

<html>
<head>
<script language=javascript>
window.location.href=''file.php'';
</script>
</head>
<body>
body content
</body>
</html>

will the JavaScript in the head execute before the body loads,



这是全局代码,应该立即执行它遇到。

It is global code and should execute immediately it is encountered.


或将

访问者在新内容之前看到内容的短暂闪现页面加载?
or will
the visitor see a short flash of the content before the new page loads?



这可能与浏览器有关 - 页面可能会在等待新请求被回答时继续加载

,或者可能会停止。

测试将确认。


-

Rob

That is likely to be browser dependent - the page may continue to load
while waiting for the new request to be answered, or it may stop.
Testing will confirm.

--
Rob


McKirahan在2007年1月3日下午7:19发表以下内容:
McKirahan said the following on 1/3/2007 7:19 PM:

" Manfred Kooistra" < ma ************** @ gmx.dewrote in message

news:11 **************** ****** @6g2000cwy.googlegrou ps.com ...
"Manfred Kooistra" <ma**************@gmx.dewrote in message
news:11**********************@6g2000cwy.googlegrou ps.com...

>如果我有这样的文件:

< html>
< head>
< script language = javascript>
window.location.href =''file.php'';
< / script>
< / head>
< body>
身体内容
< / body>
< / html>

将头部的JavaScript在主体加载之前执行,或者访问者在新页面加载之前看到内容的短暂闪存?
>If I have a document like this:

<html>
<head>
<script language=javascript>
window.location.href=''file.php'';
</script>
</head>
<body>
body content
</body>
</html>

will the JavaScript in the head execute before the body loads, or will
the visitor see a short flash of the content before the new page loads?



1)你试过吗?


2)你想要什么行为?

如果你想立即去另一个页面,你可以这样做:


< html>

< head>

< title> 1.htm< / title>

< / head>

< body onload =" window.location.href =''file.php'';">

正文内容

< / body>

< / html>


1) Did you try it?

2) What behavior do you want?

If you want to go to another page immediately, you can do this:

<html>
<head>
<title>1.htm</title>
</head>
<body onload="window.location.href=''file.php'';">
body content
</body>
</html>



该代码没有立即转到另一个页面,它等待

页面完成加载然后尝试转到另一页。最初发布的代码

会立即尝试转到另一个页面。

That code doesn''t "go to another page immediately", it waits for the
page to finish loading then it tries to go to another page. The code
originally posted will try to go to another page immediately though.


或者这个:


< html>

< head>

< title> 2.htm< / title>

<脚本类型=" text / javascript">

window.onload = function(){

window.location.href =''file.php'';

}

< / script>

< / head>

< body>

正文内容

< / body>

< / html>
Or this:

<html>
<head>
<title>2.htm</title>
<script type="text/javascript">
window.onload = function () {
window.location.href=''file.php'';
}
</script>
</head>
<body>
body content
</body>
</html>



与上面相同。

Same as above.


甚至这个(不需要启用JavaScript) ):
Or even this (which doesn''t require that JavaScript be enabled):



但它确实要求在IE中启用META重定向(它可以明确禁用
)。


-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


这篇关于文档头中的脚本何时执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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