Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready shorthand

Is the following shorthand for $(document).ready?

(function($){

//some code

})(jQuery);

I see this pattern used a lot, but I'm unable to find any reference to it. If it is shorthand for $(document).ready(), is there any particular reason it might not work? In my tests it seems to always fire before the ready event.

like image 620
Mark Brown Avatar asked Oct 13 '22 21:10

Mark Brown


People also ask

What does $( document .ready function () mean?

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.

What is replacement of $( document .ready function?

load(function(){ // ...}) @undefined, this is almost the same as $(document). ready(function(){ ... }) . load() will wait until the graphics are also loaded.

What is $( document .ready () and $( window .load () in jQuery?

ready() and $(window). load() event is that the code included inside onload function 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.

What does document ready mean?

The document ready event signals that the DOM of the page is now ready, so you can manipulate it without worrying that parts of the DOM has not yet been created. The document ready event fires before all images etc. are loaded, but after the whole DOM itself is ready.


2 Answers

The shorthand is:

$(function() {
    // Code here
});
like image 577
Kyle Trauberman Avatar answered Oct 16 '22 10:10

Kyle Trauberman


The shorthand for $(document).ready(handler) is $(handler) (where handler is a function). See here.

The code in your question has nothing to do with .ready(). Rather, it is an immediately-invoked function expression (IIFE) with the jQuery object as its argument. Its purpose is to restrict the scope of at least the $ variable to its own block so it doesn't cause conflicts. You typically see the pattern used by jQuery plugins to ensure that $ == jQuery.

like image 262
BoltClock Avatar answered Oct 16 '22 09:10

BoltClock