Here is my brief HTML document.
Why is Chrome Console noting this error:
"Uncaught TypeError: Cannot call method 'appendChild' of
null
"?
<html>
<head>
<title>Javascript Tests</title>
<script type="text/javascript">
var mySpan = document.createElement("span");
mySpan.innerHTML = "This is my span!";
mySpan.style.color = "red";
document.body.appendChild(mySpan);
alert("Why does the span change after this alert? Not before?");
</script>
</head>
<body>
</body>
</html>
This question is related to
javascript
google-chrome
Browser parses your html from top down, your script runs before body is loaded. To fix put script after body.
<html>
<head>
<title> Javascript Tests </title>
</head>
<body>
</body>
<script type="text/javascript">
var mySpan = document.createElement("span");
mySpan.innerHTML = "This is my span!";
mySpan.style.color = "red";
document.body.appendChild(mySpan);
alert("Why does the span change after this alert? Not before?");
</script>
</html>
document.body
is not yet available when your code runs.
What you can do instead:
var docBody=document.getElementsByTagName("body")[0];
docBody.appendChild(mySpan);
Your script is being executed before the body
element has even loaded.
There are a couple ways to workaround this.
Wrap your logic in an event listener for DOMContentLoaded
.
In doing so, the callback will be executed when the body
element has loaded.
document.addEventListener('DOMContentLoaded', function () {
// ...
// Place code here.
// ...
});
Depending on your needs, you can alternatively attach a load
event listener to the window
object:
window.addEventListener('load', function () {
// ...
// Place code here.
// ...
});
For the difference between between the DOMContentLoaded
and load
events, see this question.
<script>
element, and load JavaScript last:Right now, your <script>
element is being loaded in the <head>
element of your document. This means that it will be executed before the body
has loaded. Google developers recommends moving the <script>
tags to the end of your page so that all the HTML content is rendered before the JavaScript is processed.
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>Some paragraph</p>
<!-- End of HTML content in the body tag -->
<script>
<!-- Place your script tags here. -->
</script>
</body>
</html>
Or add this part
<script type="text/javascript">
var mySpan = document.createElement("span");
mySpan.innerHTML = "This is my span!";
mySpan.style.color = "red";
document.body.appendChild(mySpan);
alert("Why does the span change after this alert? Not before?");
</script>
after the HTML, like:
<html>
<head>...</head>
<body>...</body>
<script type="text/javascript">
var mySpan = document.createElement("span");
mySpan.innerHTML = "This is my span!";
mySpan.style.color = "red";
document.body.appendChild(mySpan);
alert("Why does the span change after this alert? Not before?");
</script>
</html>
Add your code to the onload event. The accepted answer shows this correctly, however that answer as well as all the others at the time of writing also suggest putting the script tag after the closing body tag, .
This is not valid html. However it will cause your code to work, because browsers are too kind ;)
See this answer for more info Is it wrong to place the <script> tag after the </body> tag?
Downvoted other answers for this reason.
Source: Stackoverflow.com