Here is my brief HTML document.
Why is Chrome Console noting this error:
\"Uncaught TypeError: Cannot call method \'appendChild\' of
nul
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.
element, and load JavaScript last:Right now, your element is being loaded in the element of your document. This means that it will be executed before the body has loaded. Google developers recommends moving the tags to the end of your page so that all the HTML content is rendered before the JavaScript is processed.
Some paragraph