Run Jquery after page has loaded

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


Following is the code that needs to be put to the header of the page (in head tags) and that we use to run jquery only after the page has loaded:

$(window).bind(“load”, function() {
   // code here
});

Following is the code we use in Jquery to run when the DOM is ready:

$(function() {
// code here
}





-1 points

whats the different between the page has loaded and DOM is ready?

Anonymous's picture
Created by Anonymous
3 points

page loaded means all of the page (eg. pictures loaded), dom is ready means the "sceleton".

Anonymous's picture
Created by Anonymous
22 points

To be completely correct second statement should be:

$(function() {
// code here
});

Anonymous's picture
Created by Anonymous
12 points

document ready means the DOM is loaded to it's completion, but doesn't necessary mean images are loaded.

document.ready is typically the preferred mechanism.

Anonymous's picture
Created by Anonymous
1 point

Sure but if you nest this inside of document.ready, you get a command that waits until the page is ready. This is important when dynamically sizing DIV areas (div height is equal to view portal minus header+footer or some such thing - if that calculates early you get no scroll bars)

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.