Right below the start of the <body> tag, I want to include a script that will, among other things, add a class to the <body> tag.
<body>
<script type="text/javascript">
if(conditionIsMet) {
document.body.className += ' condition-is-met';
}
</script>
<!--
...
...
...
-->
</body>
I know that, if I want to do heavy DOM manipulation like adding children to the document, I should use a DOM-ready function to ensure that the document tree has fully loaded. However, is it safe to access and manipulate document.body before the DOM tree has loaded?
This is completely safe. Once the tag is opened, the element has already been created.
That said, you probably shouldn’t be doing it this way.
Way don’t you put all of your Javascript code before the
bodyclosing tag?