372

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?

hungneox
  • 9,333
  • 12
  • 49
  • 66
  • 13
    possible duplicate of [window.onload vs document.ready](http://stackoverflow.com/questions/3698200/window-onload-vs-document-ready) – Pranay Rana Dec 06 '11 at 07:16
  • 1
    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 :D – Rifat Dec 06 '11 at 07:24
  • @Rifat I'm sorry, but Oyeme's format is easier to read >_ – hungneox Dec 06 '11 at 07:26
  • 1
    @eureka It's OK. You don't have to be sorry :) We both tried to help you. But, he should have mentioned the credit :P – Rifat Dec 06 '11 at 07:29
  • possible duplicate of [window.onload vs. body.onload vs. document.onready](http://stackoverflow.com/questions/3474037/window-onload-vs-body-onload-vs-document-onready) – vault Mar 17 '14 at 19:40

8 Answers8

489

$(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(){
})
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Oyeme
  • 11,088
  • 4
  • 42
  • 65
  • 26
    point to orignal post : http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/ – Pranay Rana Dec 06 '11 at 07:17
  • 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' ...) handler – Evan Langlois Sep 28 '15 at 17:49
  • 6
    `.on( "ready", handler )` - deprecated as of jQuery 1.8. see https://api.jquery.com/ready/ – TeNNoX Jan 03 '16 at 15:04
  • What's the difference between `$(document).ready` and `$(document).load`? – renatov Jan 09 '16 at 01:18
  • 1
    $(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. – pazof Jan 28 '16 at 15:17
  • This answer should be edited to read `$(window).on('load', function() {...` to comply with jQuery 3's newer deprecation of the `.load()` method. – verism Oct 31 '16 at 14:12
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

Community
  • 1
  • 1
bighearted
  • 351
  • 2
  • 2
13

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.
logi-kal
  • 7,107
  • 6
  • 31
  • 43
Bharat Chodvadiya
  • 1,644
  • 4
  • 20
  • 31
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.

Rifat
  • 7,628
  • 4
  • 32
  • 46
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
});
Demis Palma ツ
  • 7,669
  • 1
  • 23
  • 28
Shubham Kumar
  • 1,221
  • 14
  • 5
  • Difference between jQuery(document).ready(function(){ and jQuery(document).ready(function($){ ? Note the $ inside the parentheses . – MarcoZen Aug 02 '17 at 06:41
4
$(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"); 
});
Rob
  • 26,989
  • 16
  • 82
  • 98
Pavan Hukerikar
  • 175
  • 1
  • 8
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...

Kumar KS
  • 873
  • 10
  • 21
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

Srikrushna
  • 4,366
  • 2
  • 39
  • 46