Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I initialize jQuery?

I have seen this (I'm also using it):

$(document).ready(function(){
   // do jQuery
})

and also this (I have tried lately):

(function(){
   // do jQuery
})(jQuery)

both work fine.

What is the difference of the two (except on how it looks)?

Which one is more proper to use?

like image 251
Reigel Avatar asked Feb 07 '10 04:02

Reigel


People also ask

What is jQuery init?

Next the jQuery() function is called, passing in a function name ( init ). This causes the init() function to run as soon as the page's DOM is loaded: $( init ); The init() function itself uses jQuery to fade all h1 headings in the Web page out, then in.

Where do I declare jQuery?

Answers. A simple solution for anyone learning jQuery is to always declare your jQuery code inside the document ready function. This is to ensure that all of the html page elements also known as the DOM (Document Object Model) have loaded before any jQuery code is run.

What is the basic need to start with jQuery?

Before you start studying jQuery, you should have a basic knowledge of: HTML. CSS. JavaScript.

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.


3 Answers

The second example you show is a self executing anonymous function. Every separate JS file you use would probably benefit from using it. It provides a private scope where everything you declare with the var keyword remains inside that scope only:

(function($){
   var special = "nice!";
})(jQuery);

alert(special); // would be undefined

The first example is shorthand for $(document).ready which fires when the DOM can be manipulated.

A couple cool things about it. First, you can use it inside the self executing function:

(function($){
   $(function(){
      // Run on DOM ready
   });

   // Run right away
})(jQuery);

Secondly, if all you need is a few lines in document ready, you can combine both the private scope and the DOM ready function like this:

jQuery(function($){
   // $ = jQuery regardless of what it means
   // outside this DOM ready function
});
like image 53
Doug Neiner Avatar answered Oct 16 '22 09:10

Doug Neiner


The first example runs the function when the DOM tree is built. The second example runs the function right away.

If you look closely, in the second example, there are two parentheses after the function declaration ( in this particular case, you pass in the global jQuery object as an argument to avoid conflict ), thereby immediately invoking the function

The right function to use depends on when you want the function to run. If you want to run a function on DOMReady ( the ready event ), you can use $( document ).ready like you mentioned or the shorthand $( function() {...} ).

Otherwise, if you want to run a function immediately and have anonymous function scope, use the second example.

like image 37
Jacob Relkin Avatar answered Oct 16 '22 11:10

Jacob Relkin


In addition to all the previous answers, jQuery have three initialization methods that can be used:

The traditional method compatible with most browsers, see code:

$(document).ready(function () {

        });

The short-hand method, see code:

$(function () {

        });

The implicit method, see code:

$().ready(function () {

        });

They all work for modern browsers and safe to use.

like image 5
Ashraf Sada Avatar answered Oct 16 '22 11:10

Ashraf Sada