:
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: null is 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