window load inside a document ready?

Sparky picture Sparky · Feb 15, 2011 · Viewed 17.7k times · Source

Sorry if this has been answered before but all searches talk about the differences, not about using the two together, if possible.

Simply, can $(window).load.(function() {}) be used INSIDE of $(document).ready.(function() {}) ?

I have some things that should be done after the DOM is loaded but then I want to reveal certain elements only after the images have finished loading. The only thing that works in Explorer 8, is putting the $(window).load functions inside of the $(document).ready with everything else.

Is this an acceptable practice?

I just want to use the most acceptable method to reveal a DIV that contains small images, like a toolbar, after it's fully constructed. (visibility hidden versus display none, for example). The HTML for this DIV is written by the code inside the $(document).ready and then appended to the body using $('body').append() before using the $(window).load.

I'm having lots of problems with Explorer 8.

Answer

Ken Redler picture Ken Redler · Feb 15, 2011

This works fine and is an acceptable practice. After all, as you describe, there may be cases where the logic in the $(window).load() handler depends on work taking place after the DOM is ready. If the window is in fact already loaded by the time you set up $(window).load(), the handler will just fire immediately.