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! While you're at it, check out some resources Treehouse students have shared here.

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

JavaScript

what is the purpose of initializing functions within a document.ready function?

can someone explain to me what is the purpose of initializing functions within a document.ready function such as below:

$(function() {

function initScripts() {
$(document).ready(function(){
exampleFunction1();
exampleFunction2();
});

)}//end all scripts

1 Answer

It ensures that everything on the page is loaded before the function is executed. That is so there is no problems or conflicts of any sort.

A good example would be if you tried to manipulate an element but the element was not yet loaded on the page when the function was called causing problems.