Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are $(function(){}); and $("document").ready(function(){}); the same?

I've been enjoying Lynda.com's Jquery Essential Training, and I've noticed that in the beginning the instructor uses:

    Fig. 1

    $("document").ready(function(){
    fun stuff goes here
    });

However, somewhere along the line he starts using:

    Fig. 2

    $(function(){
    fun stuff goes here
    });

From the way he speaks, it sounds as if they are completely synonymous (some inherent jquery shorthand?) but as far as I can tell, it's never explicitly touched upon.

I'm sure someone could clear this up quickly for me. I found this but I believe that question is slightly different--I understand the concept of calling a function on document ready versus one that is globally available; (those functions also have names.)

The instructor uses phantom functions (I think that was the term for a function without a name,) and when typing out Fig. 2, he says "So this will be on document ready..."

like image 260
jlmakes Avatar asked Dec 08 '10 06:12

jlmakes


People also ask

Which one is equivalent to $( document .ready function?

Answer: Use the DOMContentLoaded Eventready() equivalent without jQuery.

What is the difference between $( window .load and $( document .ready functions?

The key difference between $(document). 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.

Is DOMContentLoaded the same as document ready?

ready() method differs in an important and useful way: If the DOM becomes ready and the browser fires DOMContentLoaded before the code calls . ready( handler ) , the function handler will still be executed. In contrast, a DOMContentLoaded event listener added after the event fires is never executed.

What is use of $( document .ready function ()?

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.


1 Answers

Yes, they are exactly the same, just aliases.

From the jQuery site:

All three of the following syntaxes are equivalent:

$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
like image 89
David Tang Avatar answered Sep 23 '22 16:09

David Tang