Why does this not work?
Using vanilla JavaScript to set the style on body tag?
<html> <body style="display:none;"> test </body> <script> document.getElementsByTagName("body").style.display = "block"; </script> </html>
The document. body in javascript is a direct reference to the DOM element representing the <body> portion of the page. The $() part depends on how it is used. $ could be a variable name, and () after a variable or property name attempts to call a function stored in that variable or property.
Because getElementsByTagName()
returns a NodeList, not a single element. Treat it as array:
document.getElementsByTagName("body")[0].style.display = "block";
Or even simpler in case of body
:
document.body.style.display = "block";
If you want the body
tag you can simply use document.body
. See the demo.
console.time("show body"); document.body.style.display = "none"; setTimeout(function () { console.timeEnd("show body"); document.body.style.display = "block"; }, 3000);
<h1>Hello World!</h1>
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