Is there a way to force the browser to display a page only after all of the page's contents are completely loaded (such as images, scripts, css, etc)?
Using Code Step 1: Add loader DIV tag inside body tag. This DIV helps to display the message. Step 2: Add following CSS how it is going to displaying in browser. Step 3: Add following jQuery code when to fadeout loading image when page loads.
The onload attribute fires when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). However, it can be used on other elements as well (see "Supported HTML tags" below).
You can check the document. readyState property. From MDN: Returns "loading" while the document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.
The easiest thing to do is putting a div
with the following CSS in the body:
#hideAll
{
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
background-color: white;
z-index: 99; /* Higher than anything else in the document */
}
(Note that position: fixed
won't work in IE6 - I know of no sure-fire way of doing this in that browser)
Add the DIV like so (directly after the opening body
tag):
<div style="display: none" id="hideAll"> </div>
show the DIV directly after :
<script type="text/javascript">
document.getElementById("hideAll").style.display = "block";
</script>
and hide it onload
:
window.onload = function()
{ document.getElementById("hideAll").style.display = "none"; }
or using jQuery
$(window).load(function() { document.getElementById("hideAll").style.display = "none"; });
this approach has the advantage that it will also work for clients who have JavaScript turned off. It shouldn't cause any flickering or other side-effects, but not having tested it, I can't entirely guarantee it for every browser out there.
put an overlay on the page
#loading-mask {
background-color: white;
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 9999;
}
and then delete that element in a window.onload
handler or, hide it
window.onload=function() {
document.getElementById('loading-mask').style.display='none';
}
Of course you should use your javascript library (jquery,prototype..) specific onload handler if you are using a library.
Hide the body initially, and then show it with jQuery after it has loaded.
body {
display: none;
}
$(function () {
$('body').show();
}); // end ready
Also, it would be best to have $('body').show();
as the last line in your last and main .js file.
you can also go for this.... this will only show the HTML section once javascript has loaded.
<!-- Adds the hidden style and removes it when javascript has loaded -->
<style type="text/css">
.hideAll {
visibility:hidden;
}
</style>
<script type="text/javascript">
$(window).load(function () {
$("#tabs").removeClass("hideAll");
});
</script>
<div id="tabs" class="hideAll">
##Content##
</div>
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