Reputation: 8035
$(function () {
console.log('hello');
//...
})
I saw the code like this. Don't know the purpose of adding the first line (function)? Could Someone explain this?
Upvotes: 0
Views: 266
Reputation: 16693
Wrapping you Javascript code with $(function () {
:
$(function () {
console.log( "ready!" );
});
is the same (a Shorthand version) as writing:
$(document).ready(function() {
console.log( "ready!" );
});
Which ensures the script will only run once the page Document Object Model (DOM) is ready for Javascript code to execute (Jquery docs).
Upvotes: 1
Reputation: 8894
That is just jQuery short-hand for
$(document).ready(function() { ... });
Upvotes: 1