It's a self invoking anonymous function Immediately-Invoked Function Expressions, or shorter: IIFE.
It executes immediately after it's parsed created.
It has nothing to do with any event-handler for any events (such as document.onload
).
The first pair of parenthesis ((function(){/*...*/})
) make the function an expression and the second ((function(){/*...*/})
()) calls the function that results from that evaluated expression.
This pattern is often used when trying to avoid polluting the global namespace, because all the variables used in the function are not visible outside it's scope.
This is why, maybe, you confused this construction with an event-handler for document.onload
, because it's often used as this :
(function(){
// all your code here
var foo = function(){};
window.load = foo;
// ...
})();
// foo is unreachable here (it's undefined)