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?

gbro3n
  • 6,729
  • 9
  • 59
  • 100

2 Answers2

10

Its simply shorthand for:

$(document).ready(function(){

});

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

Curtis
  • 101,612
  • 66
  • 270
  • 352
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
  • 68,902
  • 24
  • 138
  • 144