Javascript cannot call method ' AppendChild ' of NULL error

Source: Internet
Author: User
Tags html page

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.

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.