soumyajyoti
soumyajyoti

Reputation: 89

Confusion with window.load function

Is there any difference between using the functions:

 $(window).load(function() {
        //do something
  });

and

  $(window).on('load', (function() {
        //do something
  });

Basically I have a page which loads a number of images (sometimes around 100). I prefer not showing the page until all the images have loaded. What I see is, if I use the second function, sometimes the function is never executed. Cannot understand why this is happening

Upvotes: 0

Views: 32

Answers (2)

Richard
Richard

Reputation: 1038

Forget about .load(), as mentioned before, it's deprecated and removed from jquery lib.

use .on(). What it does (.on('load',function())) is simply wait until all the associated elements (binded with the url, including images) are loaded, and then content of the handler is executed.

If you're using ajax to load your images, it may not work everytime, because the window element may be concidered fully loaded during the xhr request.

If so, use .ajaxStop() instead.

for exemple:

$(document).ajaxStop(function(){
    $('img').show();
});

Upvotes: 0

KL_
KL_

Reputation: 1513

The .load() event:

This method is a shortcut for .on( "load", handler ).

Upvotes: 1

Related Questions