Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready() or $(function()) -- Which to use?

Tags:

I saw there is an answered question about whether there is a difference between using $(document).ready(function(){}) and $(function(){}) (there isn't), but my question is which is the preferred syntax and why.

I've been using jQuery for about a year and have always used the $(document).ready() syntax; but lately on SO and in some other places, I've seen the $(function()) syntax used more and more.

Is there a preferred syntax that you use and why do you use it? Do you use the shorter syntax solely to save a few characters?

Just a little more background, I'm currently starting a new app from the ground up and want to put some generally accepted best practices and standards in place.

Thanks in advance!

like image 680
David Hoerster Avatar asked Aug 18 '10 13:08

David Hoerster


People also ask

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

What is $( document ready () function Why should you use 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.

Is document ready necessary?

No, it is not necessary. You can either put the script tag right before the body closing tag or if you are supporting IE9+ you can use native JS.

Why it is recommended to use the jQuery document ready approach?

jQuery ready() MethodBecause this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs. Tip: The ready() method should not be used together with <body onload="">.


Video Answer


2 Answers

I use $(document).ready(function(){}); despite the additional typing. I just prefer having that in my code to remind me exactly when the code is going to run, and for the ability to search for 'ready' to find those sections. It's the same functionality, and I normally take shortcuts, but in this case I don't.

like image 174
Fosco Avatar answered Oct 05 '22 08:10

Fosco


You can use either, $(function() {}) is just a shortcut that does exactly the same thing.

Personally, I prefer $(function() { }) because...well, I type it a lot, and it's much quicker.

I see a lot of questions of why $(document).load() doesn't work and such because they're not using the right event...I also think$(func) takes some ambiguity out, to me. Do whichever you prefer, what's clearer to you and your team is the best choice here.

like image 40
Nick Craver Avatar answered Oct 05 '22 08:10

Nick Craver