I was thinking about the jQuery $(document).ready
event and it occurs to me that I've never seen anyone apply it on anything other than $(document)
. Is there any other legitimate use for it?
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.
It is in no way required. All it does is make sure that the DOM is loaded before trying to query for and use the elements inside the DOM (usually after dom is ready and before body. onload fires). You can use jquery perfectly fine without it.
jQuery | ready() with Examples The ready() method is an inbuilt method in jQuery which helps to load the whole page then execute the rest code. This method specify the function to execute when the DOM is fully loaded. Parameters: This method accepts single parameter function which is mandatory.
load(function(){ // ...}) @undefined, this is almost the same as $(document). ready(function(){ ... }) . load() will wait until the graphics are also loaded.
From the jQuery documentation:
The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.
They all do the same:
$(document).ready(handler) $().ready(handler) (this is not recommended) $(handler)
Source: http://api.jquery.com/ready/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With