Trigger time of document. onload

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.