The body hasn't been defined at this point yet. In general, you want to create all elements before you execute javascript that uses these elements. In this case you have some javascript in the head
section that uses body
. Not cool.
You want to wrap this code in a window.onload
handler or place it after the <body>
tag (as mentioned by e-bacho 2.0).
<head>
<title>Javascript Tests</title>
<script type="text/javascript">
window.onload = function() {
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>
See demo.
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>
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With