admin管理员组

文章数量:1302274

I have a following situation: I want to show my images at once only when the page is fully loaded because I want to avoid showing images one by one inside document ready function (they are initially hidden and want to show them at once only when document is finished loading), so I am using$(window).load(function () {});

When I use this function it runs fine and behaves as expected, but my only problem is that load function is deprecated.

For example if I use the code below without $(window).load i can see my images loaded one by one which is not an option (setTimeout is no option either).

My question: How can I achieve the same behavior without using $(window).load? Thank you!!

Sample code:

$(document).ready(function(){
 //on document ready HIDE my images 
glowHide.hide();

   //on page LOAD show all images AT ONCE (works fine but depreciated) 
   $(window).load(function(){
   glowHide.show();
   });

});

I have a following situation: I want to show my images at once only when the page is fully loaded because I want to avoid showing images one by one inside document ready function (they are initially hidden and want to show them at once only when document is finished loading), so I am using$(window).load(function () {});

When I use this function it runs fine and behaves as expected, but my only problem is that load function is deprecated.

For example if I use the code below without $(window).load i can see my images loaded one by one which is not an option (setTimeout is no option either).

My question: How can I achieve the same behavior without using $(window).load? Thank you!!

Sample code:

$(document).ready(function(){
 //on document ready HIDE my images 
glowHide.hide();

   //on page LOAD show all images AT ONCE (works fine but depreciated) 
   $(window).load(function(){
   glowHide.show();
   });

});
Share Improve this question edited Oct 5, 2012 at 12:57 Dejo Dekic asked Oct 5, 2012 at 12:46 Dejo DekicDejo Dekic 2,1264 gold badges29 silver badges51 bronze badges 11
  • So you want to show all images at once, only when all the images have been loaded fully? – Tim B James Commented Oct 5, 2012 at 12:49
  • Not something you should worry about. Serve up valid HTML and let the browser decide how best to load its images. – user229044 Commented Oct 5, 2012 at 12:49
  • Yes that is what i want to do! My code is working good but load is depreciated:( – Dejo Dekic Commented Oct 5, 2012 at 12:49
  • 3 Can you do $(window).on("load", function(){...})? I've not tried it, but on would bind the load event to the window. Figured it may be worth a shot =\ – Chase Commented Oct 5, 2012 at 12:54
  • I might try your suggestion Chase THX;) – Dejo Dekic Commented Oct 5, 2012 at 12:56
 |  Show 6 more ments

1 Answer 1

Reset to default 9

To bind the load event to the window, you should use the on method provided by jQuery.

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

$(window).on("load", function(){...})

本文标签: javascriptWhat is window load alternativeStack Overflow