8

I'm working with SignalR, and by extension, JQuery.

Some initialisation code runs inside a function block defined with the following syntax:

$(function () { 

    // ... Init code here e.g. 

    var hub = $.connection.myHub;
});

What is the functional difference here compared with just executing scripts directly within a pair of script tags?

gb2d
  • 6,160
  • 8
  • 51
  • 97

2 Answers2

10

Its simply shorthand for:

$(document).ready(function(){

});

http://api.jquery.com/ready/

Curt
  • 94,964
  • 60
  • 257
  • 340
6

$(function () is equivalent to document on ready. The function will execute everything inside the {} tags once the DOM has loaded.

An alternative way is:

$(document).ready(function() {

}); 

http://api.jquery.com/ready/

Darren
  • 63,390
  • 21
  • 126
  • 134