JS implementation of the operation successfully timed back to the homepage effect

Source: Internet
Author: User

Page code

1 <! DOCTYPE html> 2 

Issues to be aware of (sequential execution of HTML Code)

If the location of the script code is placed in front of the HTML code, the Following:

1 <! DOCTYPE html> 2 

Running the code in the browser will cause an error in the 9th line:

1 TypeError: nullis not an object (evaluating ‘document.getElementById("time").innerHTML‘)

The reason is that the HTML code is executed sequentially when the page is loaded (the function is executed when it is called) when it executes to:

1 var num=document.getelementbyid ("time"). innerHTML;

The content of the span tag with the "time" ID is not loaded, so it is suggested that the return value is Empty.

If you choose script code before the HTML code, you need to write this statement into the function body.

The *JAVASCIRPT function automatically executes when the page is loaded:

Method One:
1 <script type= "text/javascript" >2 function load () {3 alert (' Hello '); 4}5 </script>6 7 <body onload= "load ( ) ">
Method Two:
1 <script>2 window.onload = function () {3 alert ("hello"); 4}5 </script>6 7 <body>

JS implementation of the operation successfully timed back to the homepage effect

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.