Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
john larson
16,594 Pointswhich version of document.ready is this?
I know there's different ways to do it..that is, make sure the html is loaded before the JS runs. This is the most recent one I've seen.
var main = function(){
//code goes in here
}
// this goes at the bottom of the page
$(document).ready(main);
Just wondering if it's new, old or something in the middle
3 Answers

Steven Parker
224,836 Points
That's the standard JQuery ready function
I'm not sure what you mean by "version". I think all versions of JQuery have this. Perhaps you are thinking of methods for waiting that don't require JQuery, like the onload event.
Your last line can be abbreviated as just this (it does the same thing):
$(main);

john larson
16,594 PointsThanks, I like succinct.

Dan Weru
47,649 PointsHello Larson,
It's the same function you're used to. The implementation is just a little different; instead of wrapping the function in the $(document).ready() function like this,
$(document).ready(function(){
// code
});
they instantiate (save) the function in a variable, then pass the variable to it instead. When you think about it, the end result is the same.
Note that the function won't be called up until the
$(document).ready(main);
line is reached.

john larson
16,594 PointsThanks Daniel, I didn't know the ins and outs of how it worked. I appreciate the explanation.

Dan Weru
47,649 PointsYou're most welcome, anytime.
john larson
16,594 Pointsjohn larson
16,594 PointsI'm sure there's other ways also. Just seems like everyone has a different way of doing stuff, which is fascinating...