What does $(function() {} ); do?
It's just shorthand for $(document).ready()
, as in:
$(document).ready(function() {
YOUR_CODE_HERE
});
Sometimes you have to use it because your function is running before the DOM finishes loading.
Everything is explained here: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
$(function() { ... });
is just jQuery short-hand for
$(document).ready(function() { ... });
What it's designed to do (amongst other things) is ensure that your function is called once all the DOM elements of the page are ready to be used.
However, I don't think that's the problem you're having - can you clarify what you mean by 'Somehow, some functions are cannot be called and I have to call those function inside' ? Maybe post some code to show what's not working as expected ?
Edit: Re-reading your question, it could be that your function is running before the page has finished loaded, and therefore won't execute properly; putting it in $(function) would indeed fix that!
Some Theory
$
is the name of a function like any other name you give to a function. Anyone can create a function in JavaScript and name it $
as shown below:
$ = function() {
alert('I am in the $ function');
}
JQuery is a very famous JavaScript library and they have decided to put their entire framework inside a function named jQuery
. To make it easier for people to use the framework and reduce typing the whole word jQuery
every single time they want to call the function, they have also created an alias for it. That alias is $
. Therefore $
is the name of a function. Within the jQuery source code, you can see this yourself:
window.jQuery = window.$ = jQuery;
Answer To Your Question
So what is $(function() { });?
Now that you know that $
is the name of the function, if you are using the jQuery library, then you are calling the function named $
and passing the argument function() {}
into it. The jQuery library will call the function at the appropriate time. When is the appropriate time? According to jQuery documentation, the appropriate time is once all the DOM elements of the page are ready to be used.
The other way to accomplish this is like this:
$(document).ready(function() { });
As you can see this is more verbose so people prefer $(function() { })
So the reason why some functions cannot be called, as you have noticed, is because those functions do not exist yet. In other words the DOM has not loaded yet. But if you put them inside the function you pass to $
as an argument, the DOM is loaded by then. And thus the function has been created and ready to be used.
Another way to interpret $(function() { })
is like this:
Hey $ or jQuery, can you please call this function I am passing as an argument once the DOM has loaded?
The following is a jQuery function call:
$(...);
Which is the "jQuery function." $
is a function, and $(...)
is you calling that function.
The first parameter you've supplied is the following:
function() {}
The parameter is a function that you specified, and the $
function will call the supplied method when the DOM finishes loading.