I am going to do my best here to distinctly explain what my issue is without the use of jsfiddle because $(window).on("load")
does not fire within their IDE.
I have a html 'wrapper' which dynamically loads (ajax) html into div.content
.
<body>
<div class="header"></div>
<div class="overlay"></div>
<div class="loader hidden" align="center">
<img src="images/loading.gif" />
</div>
<!-- Container for the content -->
<div class="content">
<!-- dynamic content appears here -->
</div>
<div class="footer"></div>
</body>
At the start of a new piece of content being loaded, I toggle the visibility between the loading gif and the content div.
$(".content").toggleClass("hidden", true);
$(".loader").toggleClass("hidden", false);
At the end of the loading /ajax process I have the following code:
$(".content").load("screens/"+this.currentScreen+"/"+this.currentScreen + ".html .screen", function( response, status, xhr )
{
//other code
//
//
$(window).on("load", function() {
$(".content").toggleClass("hidden", false);
$(".loader").toggleClass("hidden", true);
});
});
I chose $(window).on("load")
because I only want div.content
to be visible again once all the images have finished loading, as opposed to $(document).ready()
.
For the first piece of content to be loaded, this works perfectly. However for the second piece of content, the $(window).on("load")
event never fires.
I have a hunch that this event is not allowed to be invoked a second time, or maybe it is unbound after being called?
I tried triggering / invoking the method but to no avail.
if((window.onload) === null)
{
$(window).on("load", function() {
console.log("$(window).on('load') called");
$(".content").toggleClass("hidden", false);
$(".loader").toggleClass("hidden", true);
});
}
else
{
$(window).trigger("load");
}
Also, I assure everyone that it isn't the content being loaded, because if I replace $(window).on("load")
with $(document).ready()
it works fine. I just desperately want to wait for the new images to have loaded.
How can I achieve this?