Best Windows Hosting

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 10 June 2012

Anatomy of document.ready function in jQuery

Posted on 03:20 by Unknown
All jQuery methods come inside a document.ready() function. This is to prevent any jQuery code from running before the document is finished loading (is ready).

Here are some examples of actions that can fail if functions are run before the document is fully loaded:

1. Trying to hide an element that doesn't exist
2. Trying to get the size of an image that is not loaded

document.ready syntax:

<div id="divTest1"></div>
<script type="text/javascript">
function DocumentReady()
{
        $("#divTest1").text("Hello, world!");   
}

$(document).ready(DocumentReady);
</script>
<div id="divTest2"></div>
<script type="text/javascript">
$(document).ready(function()
{
        $("#divTest2").text("Hello, world!");   
});
</script>

But of course, this syntax wasn't even short enough for the jQuery team, so they decided to create a version (overload) of the jQuery constructor which takes a ready function as a parameter, to make it even shorter:

<div id="divTest3"></div>
<script type="text/javascript">
$(function()
{
        $("#divTest3").text("Hello, world!");   
});
</script>

In the last example, our anonymous function is passed directly to the jQuery constructor, which assigns it to the ready event. As you will see when you test the code, the event is fired as soon as the page is loaded, most of the time so fast that you won't even realize it.

Difference between body onload() function used in javascript and document.ready() function used in jQuery

Document.ready() function is different from body onload() function because of 2 reasons:

1. We can have more than one document.ready() function in a page where we can have only one onload function.

2. Document.ready() function is called as soon as DOM is loaded whereas body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.

Email ThisBlogThis!Share to XShare to Facebook
Posted in jQuery | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • 13 Things to keep in mind before using DLL in Delphi
    Keep in mind the following tips when writing your DLL: 1. Make sure you use the proper calling convention (C or stdcall). 2. Know the correc...
  • How to use TADOTable in Delphi XE2?
    Following is the code snippet which will show you how to use TADOTable in Delphi XE2? procedure TClass1.GetDataFromADOTable; begin   try    ...
  • How to use FindComponent function in Delphi XE2?
    Following is the code snippet which will show you how to use FindComponent in Delphi XE2? procedure TClass1.UseFindComponent(FieldName : str...
  • Online Finance Degrees
    There is a great demand for professionals with profound knowledge of finance and accounting in most of the reputed banks and financial insti...
  • How to grab the recruiter’s attention with your resume?
    Did you know that the average recruiter spends about 8 to 10 seconds glancing at your resume before s/he moves on to the next? So, whether y...
  • 5 ways to handle workload at your workplace
    With bigger workloads, tighter deadlines and more pressure, the temptation to pack in as many tasks as possible is hard to resist. But juggl...
  • Online Marketing Degrees
    Because global competition has become so intense, it should come as no surprise that companies invest heavily in their marketing and promoti...
  • Oracle Streams: An Overview
    Oracle Streams enables information sharing. Each unit of shared information is called a message. The stream can propagate information within...
  • Frameset, Frame and IFrame Elements in HTML
    Frame Element With frames, you can display more than one HTML document in the same browser window. Each HTML document is called a frame, and...
  • 5 Tips to enjoy your workplace
    Many people dislike getting up everyday and trudge to office. This is surprising, especially when we spend more than one-third of our day at...

Categories

  • AJAX
  • C++
  • CSS
  • Delphi
  • DOTNET
  • HTML
  • Javascript
  • jQuery
  • Management
  • Online Degrees
  • Oracle
  • Others
  • Phonegap
  • PHP
  • Unix
  • XML

Blog Archive

  • ▼  2012 (155)
    • ►  September (64)
    • ►  August (11)
    • ►  July (4)
    • ▼  June (3)
      • 13 Things to keep in mind before using DLL in Delphi
      • Anatomy of document.ready function in jQuery
      • How to abort an AJAX call using jQuery?
    • ►  May (25)
    • ►  April (48)
Powered by Blogger.

About Me

Unknown
View my complete profile