Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I know what this does:

$(document).ready(function(){
    // Your code here...
});

Now I have seen people doing this lately:

<script type="text/javascript">     
$(function(){
    // Your code here...
});
</script>

Are these two ways of doing the same thing?

I see an anonymous function being declared inside a jquery selector here, but never actually being invoked, yet by the way the page runs it seems that this may just run on pageload.

like image 248
thirsty93 Avatar asked Feb 27 '09 02:02

thirsty93


People also ask

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.

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

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

Why do we start our code with document ready () in jQuery?

ready() function will load as soon as the DOM is loaded and before the page contents are loaded. You should wrap all your javascript code with this function to ensure that the code only runs when the page is fully rendered.

Is not defined in $( document .ready function ()?

To solve the "$(document). ready is not a function" error, make sure to load the jQuery library before running your JavaScript code, load jQuery only once on the page. The order we load the scripts in is very important. The scripts in your index.


1 Answers

yes, they're doing the same thing. the $() function wraps $(document).ready() when the parameter to the call is a single function object.

(Edited to reflect a question in comment)

like image 96
Ken Browning Avatar answered Oct 13 '22 01:10

Ken Browning