The dreaded document.ready() function of jQuery

If you are like me , you would have learned a little bit of Javascript and then moved on to jQuery in order to complete your project.

jQuery does make life easy for person who does not want to be get bogged down in nuances of javascript and worry about cross-platform implementation and correctness but quickly want to start working on their web projects.

The first statement that one sees ( especially when one is not from javascript background ) is

$(document).ready(function() { } );

It is intimidating to the coders who like me were just getting into jQuery. Having never worked on front end coding, even the }); was like nightmare

But actually looking back it is quite simple to understand.

$ is for telling the browser that $(document) is the version of document object of javascript that is now decorated with many functions from jQuery.

ready function is one of them which tells the browser to execute the code contained in it. Ready tells when the HTML elements in documents are all loaded from the server.

so $(document).ready() is a function called on document jquery object.

Now anything that is to be done is inside it .


$(document).ready(function(){});

and inside the function, you can do whatever you want to


$(document).ready(function(){

alert("Document is ready");

});

You can also name the function as


var fn = function(){

alert("Document is ready");

};

and then call


$(document).ready(fn);

Doesn’t look intimidating now , is it ?

Leave a Reply

Your email address will not be published. Required fields are marked *