Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $(document).ready(initialize) and $(document).on('ready', initialize)?

What is the difference between:

$(document).ready(initialize); 

and

$(document).on('ready', initialize); 

To me they seem to work in the same way.

like image 729
Lorraine Bernard Avatar asked Nov 27 '12 17:11

Lorraine Bernard


People also ask

What is the difference between document ready () and Onload () function in JavaScript?

The document.ready () function will be executed as soon as the DOM is loaded. It will not wait for the resources like images, scripts, objects, iframes, etc to get loaded. We can have multiple document.ready () function in our code but only one body.onload () is allowed. onload () will be called only when everything gets loaded.

What is $ (document).ready () equivalent in JavaScript?

What is $ (document).ready () equivalent in JavaScript? Javascript Web Development Front End Technology In jQuery, if you want an event to work on your page, you should call it inside the $ (document).ready () function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

How to achieve the same result like $ (document) ready in JavaScript?

$ (document).ready (function () { alert (“Document loaded successful!"); }); In JavaScript, to achieve the same result like $ (document).ready, try the following code −

What is the use of)(document).ready() method in jQuery?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $( document ).ready() method will run once the page DOM is ready to execute JavaScript code. You can try to run the following code to learn how to use to use $(document).ready() in jQuery:


2 Answers

TL;DR

  • $(document).on('ready', ...) is deprecated since it does not execute callbacks bound after the DOM was completely parsed. It gets event object as first argument.

  • $().ready() gets passed a reference to jQuery as first argument.


Detailed answer

$(document).on('ready', initialize);

This expression is binding a ready event handler to document, like you would expect from any other event handler. Using this to listen to DOM ready is deprecated as of jQuery 1.8:

There is also $(document).bind("ready", handler), deprecated as of jQuery 1.8. This behaves similarly to the ready method but if the ready event has already fired and you try to .bind("ready") the bound handler will not be executed. Ready handlers bound this way are executed after any bound by the other three methods above.

Note that ready is a custom event, and triggered by jQuery internally. That also means that you can trigger it manually, which might mess things up.


$(document).ready(initialize);

This expression does not really bind an event handler. jQuery.fn.ready is a dedicated method to register callbacks to be run when the DOM was completely parsed. jQuery is adding the callback to a promise object and it does not matter which selector you pass to $.

Additionally, the callback gets passed a reference to the jQuery object instead of an event object.


The follwoing part of the source code nicely shows that callbacks registered like this are handled differently:

// If there are functions bound, to execute
readyList.resolveWith(document, [jQuery]);

// Trigger any bound ready events
if (jQuery.fn.trigger) {
    jQuery(document).trigger("ready").off("ready");
}
like image 23
Felix Kling Avatar answered Oct 12 '22 02:10

Felix Kling


$(document).on('ready',initialize); will not work if the DOM is already ready when the file is executed.

$(document).ready() has a special handling for this: it ensures it is always called

like image 147
antonjs Avatar answered Oct 12 '22 03:10

antonjs