An error is always prompted when I wrote a special webpage effect yesterday. The reason is not known until today through an example.
I always think that using document. onload of JavaScript to specify a function is the same as adding onload to the body tag.
However, the following example shows that document. onload is not triggered when the page is loaded.
ExampleCodeAs follows: (test environment: 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 > Untitled document </ Title >
6 < Script Language = "JavaScript" >
7 Function Mytest () {
8Alert (document. getelementbyid ("My2"));
9}
10 Document. onload = Mytest ();
11 </ Script >
12 </ Head >
13
14 < Body >
15 < P ID = "My2" > Test content </ P >
16 </ Body >
17 </ Html >
When opening the HTM file in the example, the page is not fully displayed, that is, the prompt dialog box is displayed when the screen is white. This indicates that mytest is called when the page is not displayed completely.
Comment out the document. onload sentence and modify the body label
< Body Onload = "Mytest ()" >
Re-open the page. mytest is called only after the page is loaded. I don't know why.