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,390
  • 9
  • 57
  • 98

2 Answers2

10

Its simply shorthand for:

$(document).ready(function(){

});

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

Curtis
  • 98,395
  • 62
  • 265
  • 345
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
  • 66,506
  • 23
  • 132
  • 141