Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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?

like image 475
hungneox Avatar asked Dec 06 '11 07:12

hungneox


People also ask

What is the difference between the $( document .ready and window onload events?

The $(document). ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded.

What is difference between $( document .ready function () vs $( function ()?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

What is the difference between onload () and document ready () methods?

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.


1 Answers

$(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(){ }) 
like image 85
Oyeme Avatar answered Sep 27 '22 19:09

Oyeme