Please navigate to the bottom of the page for Table of Contents

Thursday, June 2, 2011

jQuery AJAX functions part 1–the load() method

jQuery is rapidly gaining popularity in many web applications and most of the modern frameworks (including ASP.NET) are providing official support for this lightweight and powerful framework. As such, most of the web developer and SDET interviews now make sure that the interviewee knows and understands the jQuery concepts. This includes (but is not limited to) selectors, getters, setters, events, effects, ajax, plugins and UI library concepts.

In the next few posts, we will discuss the jQuery AJAX functions. There are 3 categories of questions: Explain the basic jQuery load() method; Explain the jQuery utility functions; and lastly, the king of all functions: Explain the jQuery ajax() function. In this post we will focus our attention on the load() function.

Basic load() method

The simplest AJAX method in jQuery is the load() method; given a URL, it will asynchronously load its contents and display it dynamically on the page (in the element specified).  In it’s simplest form, the load function looks something like this:


A more complete example might be when you combine this functionality with a timer to update some element on a periodic basis.

// Load and display the current status every minute
setInterval(function() {
}, 60000);

The code above will get the entire contents of the specified web page. The load() function also allows you to get partial page content. The trick is to add a space to the URL and follow it with a jQuery selector. When the URL has loaded, the selector you specified will be used to select the portions of the loaded HTML to be displayed:

// Load the temperature section of the weather report
$('#db_status').load("CurrentStatus.html #DB");

The complete signature of the load() method takes in two additional optional parameters

.load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )

url - A string containing the URL to which the request is sent.
data - A map or string that is sent to the server with the request.
complete(responseText, textStatus, XMLHttpRequest) - A callback function that is executed when the request completes.

Passing data to the load() method

If you pass in a string as the data, it is appended as a query string parameter. On the other hand, if you pass in an object, it is converted to name=value pairs. For example, the first call is passed as query string data and the second example shows an object that is passed to the call.

// Load the status for a particular server
$('#status').load("CurrentStatus.html", "server=db01");

// pass an object as data specifying an interval of day
{ server:'DB01', interval:'1day' });

Executing a callback function

The second optional parameter is a callback function that will execute on completion of the load() method.

$('#result').load('ajax/test.html', function() {
alert('Load was performed.');

One important fact to note is that the POST method is used if data is provided as an object; otherwise, GET is assumed.


  1. I really like what you have covered whole in your jquery series.

    1. Hello Nikhil,

      What a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.
      I'd like to share with you a OOP design question I have related to a group of classes/objects I'm developing in Java.
      I'm writing a little program to generate Cards for the Italian family game of Tombola (a sort of Bingo) and then use these cards to support the game when there are many players (tens or hundreds).
      Cards are made up by 15 numbers chosen among the set [1.90] and displaced on three rows with five numbers each.
      I’d like to have a “Card” class which objects represent playing Tombola card.
      Then I’ve an algorithm that arrange all the 90 numbers (they are in an int [90] array) so that is generate six cards at one time (six cards with no number in common are called a “series of cards”). It will make sense to avoid client program could create just one single card with a statement like:
      Card card1 = new Card();
      My question is related to the “factory pattern” to use for instantiating new Card objects and the place where to put the generation algorithm introduced above.
      Ensure sufficient information (accuracy, completeness, precision, and classification of the information, where warranted) is provided or display.
      Currently I’ve just a couple of ideas.
      The first:
      I use a public static method on the Card class that returns an array containing six cards.
      Very useful article, if I run into challenges along the way, I will share them here.


  2. This is an excellent straight forward example and clearly presented explanation. Thank you.

  3. Once you know what you want to see, then you can figure out where you want to stay.
    Situs Poker Online International resorts and hotels are common around the island and are great places to stay when you want easy beach access.

    For cheaper and more authentically Cuban accommodations,
    DominoQQ you can choose from all sorts of hotels and guest houses. Whether you stay in the capital of Havana, or decide to stop in some of the smaller towns, there is always going to be somewhere you can rent a room. Known as "casa particulares", there are small guest houses where people offer rooms for rent in their own homes. It's a common feature in Cuba and a way to same some money because the costs are usually quite low.

    These are basically all the details you need before you plan your Cuba voyage.BandarQ Once you have a travel plan worked out, all that's left is getting things booked and head out.

    Terri has traveled the world and is always looking forward to her next adventure.Domino 99 She prefers to visit new and exciting places instead of the usual destinations.

  4. usually posts some incredibly exciting stuff like this. If youre new to this site :)
    That could be the finish of this report. Right here you will uncover some websites that we think youll value, just click the links.

    Agen Poker Online Indonesia Terbaik Terpercaya Terbesar 2017

  5. An impressive share! I’ve just forwarded this onto a co-worker who
    has been conducting a little homework on this. And he actually bought me dinner
    because I stumbled upon it for him… lol. So allow me to reword this….
    Thank YOU for the meal!! But yeah, thanks for spending some
    time to discuss this subject here on your web page. Parabola Tv Muslim ::: Parabola Tv Muslim ::: Parabola Tv Muslim

  6. growing muslim Entrepreneurship should be our motive. Who does wanna start business but are affraid of can take help from startup muslim. They have expertise and they also help people as well.

  7. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    animal jam | five nights at freddy's | hotmail login


  8. Over time, in the sensual massage beautiful setting in Hawaii.
    Some of the best are a peacock feather or other light bird’s
    feather, silk scarf, wool muffler, cotton balls,
    cotton knit such as a soft tee shirt, dry natural sponge, marbles, tennis ball, leaves, flowers.
    Aromatherapy should also be of benefit for improving the function of the
    actual body organs.
    Jasa SEO Indonesia Murah Terbaik dan Profesional


  9. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    hotmail sign in | red ball |

  10. Hi!How to callback ajax in jquery??

  11. a pride for me to be able to discuss on a quality website because I just learned to make an article on
    cara menggugurkan kandungan

  12. We find lots of learning after reading this very useful article . cara menggugurkan hamil