Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery load() send any data even when the requested element is not found?

When I have a code like this:

   $('#intro-posts-container').load('/ajax/load.php', function() {
      bindVoting();
   });

Does the load send the request to load.php, if the #intro-posts-container element doesn't exist?

like image 926
Frantisek Avatar asked Dec 12 '12 05:12

Frantisek


People also ask

What is the load () method used for in jQuery?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.

What is the use of load () function?

The Load function initializes a database and loads it from an input data file. It can be used for initial loading of a database, as part of a database reorganization, or for reloading a database after changing the DBD definition.

What is the difference between jQuery get () and jQuery load ()?

get() executes an Ajax GET request. The returned data (which can be any data) will be passed to your callback handler. $(selector). load() will execute an Ajax GET request and will set the content of the selected returned data (which should be either text or HTML).

Is jQuery load deprecated?

The load() method was deprecated in jQuery version 1.8 and removed in version 3.0. Use the on() or trigger() method instead.


1 Answers

A quick test in firebug on this page shows that it does not send the request if the element doesn't exist.

I've verified this from the source (v1.8.3), which contains the following:

jQuery.fn.load = function( url, params, callback ) {
    // [snip]

    // Don't do a request if no elements are being requested
    if ( !this.length ) {
        return this;
    }

    // [snip]
}
like image 67
Alconja Avatar answered Sep 22 '22 15:09

Alconja