Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $(window).load and $(document).ready?

I have been having a problem lately with my JavaScript CODE and taking a portion of my code out of my $(document).ready() and putting it within $(window).load() fixed the problem.

Now I understand that window.load is fired just after document.ready, but why is it not ready after document.ready, that is after window.load()?

like image 960
Mark McCook Avatar asked Mar 03 '11 14:03

Mark McCook


People also ask

What is difference between $( function () and document Ready?

There is no difference in functionality between your examples - they both bind to DOM ready. For reference, there are two points at which you can bind your jQuery code. The first will execute when the DOM is ready (both are equivalent): // full example $(document).

What is the difference between $( window .load and $( document .ready function in jQuery Mcq?

The major difference between the JavaScript's onload and jQuery's $(document). ready(function) method is that: The onload executes a block of code after the page is completely loaded while $(document). ready(function) executes a block of code once the DOM is ready.

What is the difference between document ready and onload?

The main differences between the two are: Body. Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document. ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded.

What is the difference between window onload and document onload in JavaScript?

The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.


3 Answers

load is called when all assets are done loading, including images. ready is fired when the DOM is ready for interaction.

From the MDC, window.onload:

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.

From the jQuery API documentation, .ready( handler ):

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

like image 176
Martin Jespersen Avatar answered Oct 25 '22 22:10

Martin Jespersen


$(document).ready() means that the DOM of your page is ready to be manipulated.

window.load() is triggered when the whole page (incl. components like CSS and image files) has been completely loaded.

What are you trying to achieve?

like image 43
Mario Fink Avatar answered Oct 25 '22 22:10

Mario Fink


$(document).ready(function(){
//code here
});

The code above is used almost every time when we work with jQuery.

This code is used when we want to initialize our jQuery codes after the DOM is ready.

$(window).load()

Sometimes you want to manipulate pictures. For example you want to vertically and horizontally align a picture and you need to get the width and height of the picture in order to do that. With $(document).ready() you won’t be able to do that if the visitor doesn’t have the image already loaded, in which case you need to initialize the jquery alignment function when the image finishes loading. That’s where we use $(window).load()

like image 8
SimonGates Avatar answered Oct 25 '22 22:10

SimonGates