Languages
[Edit]
EN

JavaScript - display web page after all resources are loaded

0 points
Created by:
Hayley-Mooney
677

In this article, we would like to show you how to display web page after all resources are loaded using JavaScript.

Quick solution:

  • set body visibility to hidden: <body style="visibility: hidden">,
  • on window load event change visibility to visible: document.body.style.visibility = 'visible'.

 

Practical example

In this section, we present a full working example of how to display web page after all resources are loaded.

// ONLINE-RUNNER:browser;

<!DOCTYPE html>
<html>
<body style="visibility: hidden">
  <img src="https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png" height="50px"/>
  <div>Image description...</div>
  <iframe src="https://dirask.com/about"></iframe>
  <script>

    window.addEventListener('load', function() {
        document.body.style.visibility = ''; // removes visibility style from body
    });

  </script>
</body>
</html>

Note:

By setting the visibility to an empty string (''), we remove it from the body. Removing visibility in fact, sets the property to its default value, so the following line: 

document.body.style.visibility = '';

is equivalent to:

document.body.style.visibility = 'visible'

 

See also

  1. JavaScript - load events order for window, document, dom, body and elements

References

  1. Window: load event - Web APIs | MDN
  2. visibility - CSS: Cascading Style Sheets | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join