人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

June 18, 2007

window.onload and body onload =

Filed under: ASP.NET

本文测试环境为IE7,vs2005
<html>
<head>
<title>Untitled Page</title>
<script type=”text/javascript”>
function pageLoaded()
{
window.status=”Page loaded”;
}

</script>
</head>
<body onload=”pageLoaded();”>
<script type=”text/javascript”>
window.onload=windowLoad;

function windowLoad()
{
window.status=”Window loaded”;
}
</script>
</body>
</html>
当这个html页面被加载时,windowLoad()会被执行,而pageLoaded()不会被执行.
如果把JavaScript代码全部放在<Head>中
<html>
<head>
<title>Untitled Page</title>
<script type=”text/javascript”>
function pageLoaded()
{
window.status=”Page loaded”;
}
window.onload=windowLoad;

function windowLoad()
{
window.status=”Window loaded”;
}
</script>
</head>
<body onload=”pageLoaded();”>
</body>
</html>
则pageLoaded()执行而windowLoad()不执行.

MSDN对onload有如下描述
The onload attribute of the body object sets an onload event handler for the window.

可见,在IE解析html文件时,后出现的设定会覆盖前边的设定.即window.onload
和onload=” “,后出现者起作用.

如果要两者都执行,则要使用attachEvent来指定window load时要运行的函数:
if (document.all){
window.attachEvent(’onload’,FuncName)
}
else{
window.addEventListener(’load’,FuncName,false);
}

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2007/06/18/windowonload-and-body-onload/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham