昨天在写一个网页特效时,总是提示错误。一直到今天通过一个示例才知道原因。
我一直认为使用javascript的document.onLoad指定一个函数,跟在Body标签中加入onLoad是一样的
不过能过今天的示例发现,document.onLoad并不是在页面加载完成时引发。
示例代码如下:(测试环境Win2003 + IE7)
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html>
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
5
<title>无标题文档</title>
6
<script language="JavaScript">
7
function mytest(){
8
alert(document.getElementById("my2"));
9
}
10
document.onLoad = mytest();
11
</script>
12
</head>
13
14
<body>
15
<p id="my2">测试内容</p>
16
</body>
17
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

打开示例中的htm文件时,在页面没有完全显示,即白屏的时侯就弹出提示对话框了。说明mytest在页面没有显示完成时就被调用。
注释掉document.onLoad这句,然后修改body标签为

重新打开页面,此时才是页面加载完成后才调用mytest。不知道什么原因,呵呵。