最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide body until it finishes loading - Stack Overflow

programmeradmin3浏览0评论

I created this code so my page would be hidden until it finishes loading. But my code doesn't work as I expected. I expected this to hide the BODY until the OnLoad event was triggered. However, instead, it just stays hidden.

Any help would be appreciated, if there is maybe another, better method of hiding the BODY until it finishes loading, or what's wrong with this one.


Here's what I've tried so far:

function unveil() {
  var thebod = document.getElementById("testbody");
  thebod.STYLE = "display: block;"
}
<HTML>

<HEAD>
  <TITLE>HELLO</TITLE>
</HEAD>

<BODY ID="testbody" ONLOAD="unveil();" STYLE="display: none;">
  <div align="CENTER">
    HELLO WORLD!
  </div>
</BODY>

</HTML>

I created this code so my page would be hidden until it finishes loading. But my code doesn't work as I expected. I expected this to hide the BODY until the OnLoad event was triggered. However, instead, it just stays hidden.

Any help would be appreciated, if there is maybe another, better method of hiding the BODY until it finishes loading, or what's wrong with this one.


Here's what I've tried so far:

function unveil() {
  var thebod = document.getElementById("testbody");
  thebod.STYLE = "display: block;"
}
<HTML>

<HEAD>
  <TITLE>HELLO</TITLE>
</HEAD>

<BODY ID="testbody" ONLOAD="unveil();" STYLE="display: none;">
  <div align="CENTER">
    HELLO WORLD!
  </div>
</BODY>

</HTML>

Share asked Nov 23, 2016 at 15:57 cascading-stylecascading-style 63810 silver badges24 bronze badges 1
  • try to use a class, and after loaded, remove the class. – Joao Polo Commented Nov 23, 2016 at 15:59
Add a ment  | 

4 Answers 4

Reset to default 3

The DOMContentLoaded event of the window object can do this. But, don't hide the body, hide a wrapper instead. And, when you set the style, make sure to set the style of a CSS property, not the style object itself.

window.addEventListener("DOMContentLoaded", function(){
  document.getElementById("wrapper").style.display = "block";
});
#wrapper { text-align:center; background:#e0e0e0; display:none;}
<HTML>

<HEAD>
  <TITLE>HELLO</TITLE>
</HEAD>

<BODY>
  <div id="wrapper">
    HELLO WORLD!
    
    <!-- The following is only added to create a delay in the 
         parsing of the document -->
    <script>
      for(var i = 0; i < 100000000; ++i){ var x = i / 3.14; }
    </script>  
  </div>
</BODY>

</HTML>

You're not setting the elements 'style' correctly:

You can either do:

element.style.display = "block";

Or

element.setAttribute('style', "display: block");

Here is a working example:

function unveil() {
  var thebod = document.getElementById("testbody");
  thebod.style.display = "block";
}
<HTML>

<HEAD>
  <TITLE>HELLO</TITLE>
</HEAD>

<BODY ID="testbody" ONLOAD="unveil();" STYLE="display: none;">
  <div align="CENTER">
    HELLO WORLD!
  </div>
</BODY>

</HTML>

Your issue is here:

thebod.STYLE = "display: block;"

which should read:

thebod.style.display = 'block';

Here is the plete approach (using unobtrusive javascript):

var body = document.getElementsByTagName('body')[0];

function unveil() {
    body.style.display = 'block';
}

window.addEventListener('load', unveil, false);
body {
display: none;
}

div {
text-align: center;
}
<div>HELLO WORLD!</div>

<HTML>

<HEAD>
  <TITLE>HELLO</TITLE>
</HEAD>

<BODY ID="testbody" onload="testbody.style.display = '';" style="display: none;">
  <div align="CENTER">
    HELLO WORLD!
  </div>
</BODY>

</HTML>

发布评论

评论列表(0)

  1. 暂无评论