Conceal the content until it has completed loading

I attempted to develop a code that would hide my page until it has fully loaded. However, the code I created did not produce the desired outcome. Instead of hiding the BODY element until the OnLoad event occurred, it remained hidden at all times.

If anyone could provide assistance and suggest a more effective method for concealing the BODY while it loads or identify what is wrong with the current approach, it would be greatly appreciated.


This is what I have experimented with 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>

Answer №1

To achieve this, utilize the DOMContentLoaded event associated with the window object. However, it is recommended to hide a wrapper element instead of directly manipulating the body. Additionally, ensure that when modifying the style, you target specific CSS properties rather than the entire style object.

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>

Answer №2

The proper way to set the style of elements is essential:

You have two options:

element.style.display = "block";

Alternatively, you can use this method:

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

Here's a functional demonstration:

function reveal() {
  var body = document.getElementById("exampleBody");
  body.style.display = "block";
}
<HTML>

<HEAD>
  <TITLE>GREETINGS</TITLE>
</HEAD>

<BODY ID="exampleBody" ONLOAD="reveal();" STYLE="display: none;">
  <div align="CENTER">
    GREETINGS WORLD!
  </div>
</BODY>

</HTML>

Answer №3

Your error lies in this section:

thebod.STYLE = "display: block;"

The correct syntax should be:

thebod.style.display = 'block';

Below is a comprehensive solution using unobtrusive javascript:

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

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

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

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

Answer №4

<HTML>

<HEAD>
  <TITLE>Greetings</TITLE>
</HEAD>

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

</HTML>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error in Node REPL: Trying to access property '0' of undefined after adding a property to Object.prototype

After adding the name property to Object.prototype and attempting to reference Object.prototype, an error is encountered: TypeError: Cannot read property '0' of undefined" Although I am able to access Object.prototype.name, it seems like the na ...

Using jQuery to Convert CSV Data into an HTML Table

I've been exploring the incredible jquery.csvtotable.js plugin for converting csv files to html tables. One question that has been on my mind is how can I allow users to select a .csv file using a browse button that isn't a server-side control? ...

Steps for displaying the contents of a file from Firebase storage on a React component

I uploaded a file to Firebase storage, and I want my app to be able to access and display the contents of that file within my class component div. Currently, when I try to access the file, it just opens in a new tab instead. class ScanResult extends Comp ...

Transform a C# model into an Angular model

I'm just starting out with angularjs (started today...) and I'm having trouble passing a model from my c# controller to an angularjs controller. It appears that I need to call a get method in my angular controller to fetch data from the c# contr ...

How can I create a mipmap for a planet using three.js?

Recently, I delved into the realm of mipmapping and its definition, but I find myself uncertain about how to effectively implement this technique in three.js. After exploring a couple of examples like: and also this one: Both examples appear to utilize ...

Looking for a jQuery script that can help compare if a specific div has a scrolltop value of 0 on a parallax website

I need a jquery code that allows me to check on a parallax page whether a specific div's scrolltop is at 0. $(window).bind('scroll', function() { if ($(window).scrollTop() > 500) { $('#home').css({position: ' ...

Managing the verification of data existence in the ExpressJS service layer or controller

Working on a medium-sized website, I realized the importance of writing maintainable code with a better project structure. After stumbling upon this insightful article and some others discussing the benefits of 3-layer architecture, I found the concept qu ...

Checked boxes in the React dynamic checkbox list are not being marked

Can you please assist me with the following issue: I am in the process of creating a nested dynamic list of checkboxes for selecting companies and groups within those companies. Upon investigation, I have come up with the following code snippet const [ch ...

Does JavaScript work in Firefox, but not in IE or Chrome browsers?

I am encountering an issue with the JavaScript on my website: $(function() { var $cells = $("td"); $("#search").keyup(function() { var val = $.trim(this.value).toUpperCase(); if (val === "") { $cells.parent().show(); ...

Tap on the child to reveal their parent

I am working with a family tree that includes dropdown menus containing the names of parents and children. Each child has a link, and when I click on a child's link, I want their father to be displayed in the dropdown menu as the selected option. Can ...

Distribute progress bar evenly around a circular path

I am working on a component in ReactJS that involves a circle in the center displaying the average (rendered as a div element), with 12 progress bars aligned around it at equal angles. To achieve this, I am utilizing React Bootstrap ProgressBar. <Progr ...

The functionality of the jQuery addClass method is failing to work when implemented on

Hello, I'm currently attempting to implement a CSS Style on an <img> tag that does not have any assigned class or id. Below is my code snippet: ===CSS=== .tgll-navi { //custom class for navigation bar .navi-left { //.navi-left float:right; ...

Is there a specific method to access a JSON file with (js/node.js)?

Searching for a way to access user information stored in a JSON file using the fs module in node.js. Specifically looking to read only one user at a time. app.get("/1", function(req, res) { fs.readFile("users.json",function(data, err){res.write(data)}} W ...

Sequelize doesn't delay for the function's return value

When working with Sequelize and implementing a where condition, I need to utilize a function that will provide an array like [118, 116, 125]. Here is the code snippet: function getQuestionForQuickPreparation(req, res){ let user_id = req.params.id; async ...

Ensure that the div and table header remain in a fixed position when scrolling

I have a container with a dropdown at the top and a table at the bottom. I am implementing ngx-infinite-scrolling feature, and I want the container and the table header to remain fixed when scrolling down as more data is loaded. You can view the jsfiddle ...

Display an image with a colored background using CSS

Most of you have probably noticed that images on Facebook come with a default background. How can I achieve a similar background color for images without using an additional div just for the background? My current codes do create a background color, but th ...

What methods are available in JavaScript for determining the width of a section and implementing proper spacing for a menu?

Is it possible to use Javascript to dynamically adjust the size of each <li> element within the menuArea so that they fill the entire space correctly? // Could we achieve this with the following code... var menuSpace = Math.floor((menuAreaWidth ...

Can each `InstancedBufferGeometry` instance have its own set of unique vertex colors?

Is it possible to assign different vertex colors to each instance of InstancedBufferGeometry? const xRes = 3; const yRes = 2; const numVertices = xRes * yRes; geometry = new THREE.InstancedBufferGeometry(); geometry.copy(new THREE.PlaneBufferGeometry(3, 2 ...

The issue with the max-height transition not functioning properly arises when there are dynamic changes to the max-height

document.querySelectorAll('.sidebarCategory').forEach(el =>{ el.addEventListener('click', e =>{ let sub = el.nextElementSibling if(sub.style.maxHeight){ el.classList.remove('opened&apos ...

The jquery function is not functioning properly within a javascript code block, however it performs perfectly when placed outside

In my php page, I have included jQuery and a script.js file. When I insert this function into my php file, it runs successfully: <script> $('#scoreboard-overview').load('getusers.php?q=<?php echo $NewString; ?>').fadeIn ...