Booonz
Booonz

Reputation: 95

document.ready runs even if window.load is loaded

What to write to check if window is fully loaded then do some functions and if not then do document.ready functions....i.e : i have a preload image which appears if windows isn't fully loaded and i call it in document.ready.....and if window is fully loaded i hide this preload image....it runs correctly if window isn't fully loaded and disappear after window get loaded.....but document.ready get called even if window is already loaded ...so how to check if window is loaded then don't do document.ready and if not then do document.ready

$(document).ready(function (e) {
    $("#loding").css("visibility", "visible");
    $("body").css('overflow', 'hidden');
    $("#container").css("opacity", 0);
});
$(window).load(function (e) {
    $("#loding").animate({
        opacity: 0
    }, 500, function () {
        $("#loding").css("width", "70%");
        $("#loding").css("visibility", "hidden");
    });
    $("#container").css("opacity", 1);
    $("body").css('overflow', 'auto');
});

Upvotes: 0

Views: 272

Answers (1)

Manse
Manse

Reputation: 38147

You could do something like this :

var loaded = false;
$(document).ready(function(e) {
   if(!loaded) {
     // only run here if the $(window).load() function hasnt run
   }

});
$(window).load(function(e) {
   // run your functions
   loaded = true;
});

uses a simple boolean to indicate if the loaded functions have run or not

Upvotes: 3

Related Questions