sriram
sriram

Reputation: 33

Window.onload event and $(document).ready()

I am learning jQuery. Could someone please explain what the difference between the window.onload event and $(document).ready() in jQuery is?

Regards, JN

Upvotes: 1

Views: 3646

Answers (1)

Joris Ooms
Joris Ooms

Reputation: 12038

The difference between window.onload and $(document).ready() is explained in the jQuery tutorial

I quote:

The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:

window.onload = function(){ alert("welcome"); }

Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code. To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event.

Upvotes: 6

Related Questions