Look at the code
The code is as follows |
Copy Code |
<title>js practice</title> <body> <script src= "Script.js" ></script> <div id = "main" > <p>this is a very simple HTML page.</p> <p>it ' s about as basic as they come. It has: </p> <ul> <li>an H1 tag</li> <li>two paragraphs</li> <li>an unordered list</li> </ul> </div> <div id= "JavaScript" > </div> </body> |
Only in Google browser this error, other IE series, ff,opera are normal.
Look for a reason, JS's
The code is as follows |
Copy Code |
Document.body.appendChild (container); |
is run before the body, this time, document.body does not exist, that is null, so document.body.appendChild will report the above error.
Solution:
Include the script code inside the <body></body> or put it behind the body tag to work properly.
Modified Code
The code is as follows |
Copy Code |
<title>js practice</title> <body> <div id = "main" > <p>this is a very simple HTML page.</p> <p>it ' s about as basic as they come. It has: </p> <ul> <li>an H1 tag</li> <li>two paragraphs</li> <li>an unordered list</li> </ul> </div> <div id= "JavaScript" > </div>
<!--now it'll run after the above elements have been created--> <script src= "Script.js" ></script> </body> |
You are in the Gg browser to try it will not appear JavaScript cannot call method ' AppendChild ' of NULL error Ah, no.