jQuery - What are differences between $(document).ready and $(window).load?
Asked Answered
C

8

374

What are differences between

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

and

$(window).load(function(){
  //my code here
});

And I want to make sure that:

$(document).ready(function(){

}) 

and

$(function(){

}); 

and

jQuery(document).ready(function(){

});

are the same.

Can you tell me what differences and similarities between them?

Cordero answered 6/12, 2011 at 7:6 Comment(5)
possible duplicate of window.onload vs document.readyVerenaverene
lolz, he copied from the same link after me (without mentioning) and got accepted :P Lesson learned, don't explain to developers, they like to see code instead :DAlton
@Alton I'm sorry, but Oyeme's format is easier to read >_<Cordero
@eureka It's OK. You don't have to be sorry :) We both tried to help you. But, he should have mentioned the credit :PAlton
possible duplicate of window.onload vs. body.onload vs. document.onreadyDilatation
O
492

$(document).ready(function() {
  // executes when HTML-Document is loaded and DOM is ready
  console.log("document is ready");
});


$(window).load(function() {
  // executes when complete page is fully loaded, including all frames, objects and images
  console.log("window is loaded");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Query 3.0 version

Breaking change: .load(), .unload(), and .error() removed

These methods are shortcuts for event operations, but had several API limitations. The event .load() method conflicted with the ajax .load() method. The .error() method could not be used with window.onerror because of the way the DOM method is defined. If you need to attach events by these names, use the .on() method, e.g. change $("img").load(fn) to $(img).on("load", fn).1

$(window).load(function() {});

Should be changed to

$(window).on('load', function (e) {})

These are all equivalent:

$(function(){
}); 

jQuery(document).ready(function(){
});

$(document).ready(function(){
});

$(document).on('ready', function(){
})
Octangle answered 6/12, 2011 at 7:13 Comment(6)
point to orignal post : 4loc.wordpress.com/2009/04/28/documentready-vs-windowloadVerenaverene
The latter is preferred as the others are deprecated. Also, the latter (I believe) specifically allows multiple handlers so you can have multiple scripts all have a .on('ready' ...) handlerThereabouts
.on( "ready", handler ) - deprecated as of jQuery 1.8. see api.jquery.com/readyVeronicaveronika
What's the difference between $(document).ready and $(document).load?Merras
$(document).ready - "executes when HTML-Document is loaded and DOM is ready" - so it will run immediately after the document is loaded, not waiting for any pictures to load, or frames, objects or whatever is not yet loaded. $(document).load - "executes when complete page is fully loaded, including all frames, objects and images" - so it will wait for EVERYTHING to load - document, DOM, images, scripts, frames, objects, whatever - and then and only then will it run.Thionic
This answer should be edited to read $(window).on('load', function() {... to comply with jQuery 3's newer deprecation of the .load() method.Matrimonial
L
35

document.ready is a jQuery event, it runs when the DOM is ready, e.g. all elements are there to be found/used, but not necessarily all the content.
window.onload fires later (or at the same time in the worst/failing cases) when images and such are loaded. So, if you're using image dimensions for example, you often want to use this instead.

Also read a related question:
Difference between $(window).load() and $(document).ready() functions

Legnica answered 6/12, 2011 at 7:15 Comment(0)
G
14

These three functions are the same:

$(document).ready(function(){

}) 

and

$(function(){

}); 

and

jQuery(document).ready(function(){

});

here $ is used for define jQuery like $ = jQuery.

Now difference is that

  • $(document).ready is jQuery event that is fired when DOM is loaded, so it’s fired when the document structure is ready.
  • $(window).load event is fired after whole content is loaded like page contain images,css etc.
Goodtempered answered 5/4, 2013 at 11:8 Comment(0)
A
12

From the jQuery API Document

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.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.


Answer to the second question -

No, they are identical as long as you are not using jQuery in no conflict mode.

Alton answered 6/12, 2011 at 7:11 Comment(0)
I
7

The Difference between $(document).ready() and $(window).load() functions is that the code included inside $(window).load() will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the document ready event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.


$(document).ready(function(){

}) 

and

$(function(){

});

and

jQuery(document).ready(function(){

});

There are not difference between the above 3 codes.

They are equivalent,but you may face conflict if any other JavaScript Frameworks uses the same dollar symbol $ as a shortcut name.

jQuery.noConflict();
jQuery.ready(function($){
 //Code using $ as alias to jQuery
});
Isolating answered 6/12, 2015 at 14:50 Comment(1)
Difference between jQuery(document).ready(function(){ and jQuery(document).ready(function($){ ? Note the $ inside the parentheses .Despoliation
M
5
$(document).ready(function(e) { 
    // executes when HTML-Document is loaded and DOM is ready  
    console.log("page is loading now"); 
});

$(document).load(function(e) { 
    //when html page complete loaded
    console.log("completely loaded"); 
});
Mcgovern answered 10/11, 2017 at 9:16 Comment(2)
Please explain your answer as well.Helpmate
Copied from 4loc.wordpress.com/2009/04/28/documentready-vs-windowload (as well as the accepted answer).Ecclesiastic
G
3

The ready event is always execute at the only html page is loaded to the browser and the functions are executed.... But the load event is executed at the time of all the page contents are loaded to the browser for the page..... we can use $ or jQuery when we use the noconflict() method in jquery scripts...

Graphite answered 21/11, 2014 at 8:37 Comment(0)
G
1

$(window).load is an event that fires when the DOM and all the content (everything) on the page is fully loaded like CSS, images and frames. One best example is if we want to get the actual image size or to get the details of anything we use it.

$(document).ready() indicates that code in it need to be executed once the DOM got loaded and ready to be manipulated by script. It won't wait for the images to load for executing the jQuery script.

<script type = "text/javascript">
    //$(window).load was deprecated in 1.8, and removed in jquery 3.0
    // $(window).load(function() {
    //     alert("$(window).load fired");
    // });

    $(document).ready(function() {
        alert("$(document).ready fired");
    });
</script>

$(window).load fired after the $(document).ready().

$(document).ready(function(){

}) 
//and 
$(function(){

}); 
//and
jQuery(document).ready(function(){

});

Above 3 are same, $ is the alias name of jQuery, you may face conflict if any other JavaScript Frameworks uses the same dollar symbol $. If u face conflict jQuery team provide a solution no-conflict read more.

$(window).load was deprecated in 1.8, and removed in jquery 3.0

Glynn answered 14/6, 2018 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.