Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why the $(function () executed always

I am wondering why the $(function () {} is getting executed all the time. function test(0 is not. what's the difference between those two?

like image 908
user705414 Avatar asked May 24 '11 10:05

user705414


2 Answers

jQuery's extreme shorthand tends to trick the eye sometimes.

Look closely at the construct: A function named $ gets called, with the function as an argument. This is not equal to defining a function for later use like function test() { .... }

$ is jQuery's "document ready" shortcut. The function passed to it will get executed once the document is loaded.

like image 165
Pekka Avatar answered Nov 08 '22 21:11

Pekka


this is a short form for document ready.

$("document").ready(function(){});

so it will execute every time document loads

like image 42
sushil bharwani Avatar answered Nov 08 '22 21:11

sushil bharwani