The Wayback Machine - https://web.archive.org/web/20241122225435/https://www.geeksforgeeks.org/jquery-load-method/
Open In App

jQuery load() Method

Last Updated : 07 Jul, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

jQuery load() method is a simple but very powerful AJAX method. The Load() method in jQuery helps to load data from the server and returned it to the selected element without loading the whole page. 

Syntax:

$(selector).load(URL, data, callback);

Parameters: This method accepts three parameters as mentioned above and described below:

  • URL: It is used to specify the URL which needs to load.
  • data: It is used to specify a set of query key/value pairs to send along with the request.
  • callback: It is the optional parameter which is the name of a function to be executed after the load() method is call.

Example 1: The geeks.txt file is stored on the server and it will load after clicking the click button. The content of geeks.txt are: Hello GeeksforGeeks!

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
 
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("#div_content").load("gfg.txt");
            });
        });
    </script>
    <style>
        body {
            text-align: center;
        }
 
        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
 
        .geeks {
            font-size: 17px;
            color: black;
        }
 
        #div_content {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
    </style>
</head>
 
<body>
    <div id="div_content">
        <div class="gfg">
            GeeksforGeeks
        </div>
        <div class="geeks">
            A computer science portal for geeks
        </div>
    </div>
    <button>
        Change Content
    </button>
</body>
 
</html>


Output: 

There is also an additional callback function in the parameter which will run when the load() method is completed. This callback function has three different parameters:

  • parameter 1: It contains the result of the content if the method call succeeds.
  • parameter 2: It contains the status of the call function.
  • parameter 3: It contains the XMLHttpRequest object.

Example 2:  An alert box will appear after clicking on the button and if the content loaded successfully then it will give a message “Content loaded successfully!”. Otherwise, it will show an error message.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
 
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("#div_content").load("gfg.txt", function (response,
                    status, http) {
                    if (status == "success")
                        alert("Content loaded successfully!");
                    if (status == "error")
                        alert("Error: " + http.status + ": "
                            + http.statusText);
                });
            });
        });
    </script>
    <style>
        body {
            text-align: center;
        }
 
        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
 
        .geeks {
            font-size: 17px;
            color: black;
        }
 
        #div_content {
            font-size: 40px;
            font-weight: bold;
            color: green;
        }
    </style>
</head>
 
<body>
    <div id="div_content">
        <div class="gfg">
            GeeksforGeeks
        </div>
        <div class="geeks">
            A computer science portal for geeks
        </div>
    </div>
    <button>
        Change Content
    </button>
</body>
 
</html>


Output:



Previous Article
Next Article

Similar Reads

jQuery UI Tabs load() Method
jQuery UI consists of GUI widgets, visual effects, and themes implemented using HTML, CSS, and jQuery. jQuery UI is great for building UI interfaces for the webpages. jQuery UI tabs widget helps us to put some content in different tabs and allows us to switch between them. In this article, we will see how to use the load() method in jQuery UI slide
2 min read
jQuery Mobile Pagecontainer load() Method
jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be using the jQuery Mobile Pagecontainer load() method to load the page from the specified URL. Syntax: Invoking the load method: $(".selector").pagecontainer("load"); Loading an external p
3 min read
JQuery | Set focus on a form input text field on page load
The task is to set the focus on form input text field using jQuery. To do so, Here are a few of the most used techniques discussed. First focus() method needs to be discussed. jQuery focus() MethodThe focus event happens when an element gets focus (Either selected by a mouse click or by "tab-navigating" to it). This method triggers the focus event
2 min read
How to load external HTML file using jQuery ?
In this article, we will learn how to load an external HTML file into a div element. The following jQuery functions are used in the example codes. ready(): The ready event occurs when the DOM (document object model) has been loaded.load(): The load() method loads data from a server and gets the returned data into the selected element. Note: We will
3 min read
How to load local jQuery file in case CDN is not available ?
jQuery is a lightweight, "write less, do more" JavaScript library. jQuery helps to write JavaScript as simply as easily possible. It is effective in converting a few lines of code in JavaScript to one single line. It also simplifies tasks like Ajax calls and DOM (Document Object Model). The jQuery library contains the following features: HTML/DOM m
3 min read
How to load jQuery locally when CDN fails ?
In this article, we will learn how to load our local jQuery file when CDN fails. First of all, why we should use CDN if there is a chance that it fails? There are many advantages of using CDN rather than local jQuery files. Some advantages of using CDN are as follows. It reduces the load from our server.jQuery framework loads faster from these CDN,
3 min read
jQuery Mobile Pagecontainer load Event
jQuery Mobile is a web-based technology that can be used to make responsive content for websites that can be accessed on all types of smartphones, tablets, and desktops. In this article, we will be using the jQuery Mobile Pagecontainer load event. This event is triggered after the page is successfully loaded and inserted into the DOM. Syntax: Initi
3 min read
How to load jQuery code after loading the page?
In this article, we are going to discuss the different ways of loading the jQuery code after loading the page. The methods to accomplish this task are listed below: Table of Content Using the on() method with the load eventUsing the ready() methodUsing the load() methodMethod 1: Using the on() method with the load eventThe on() method in jQuery is
3 min read
How to Load an HTML File into Another File using jQuery?
Loading HTML files into other HTML files is a common practice in web development. It helps in reusing HTML components across different web pages. jQuery is a popular JavaScript library, that simplifies this process with its .load() method. Approach 1: Basic Loading with .load() MethodThe .load() method in jQuery is used to load data from the server
2 min read
How to load and validate form data using jQuery EasyUI ?
EasyUI is a HTML5 framework for using user interface components based on jQuery, React, Angular and, Vue technologies. It helps in building features for interactive web and mobile applications saving a lot of time for developers. It helps in building features for interactive web and mobile applications saving a lot of time for developers. Download
4 min read
How to load more feature using jQuery ?
Bootstrap provides a lot of useful features that we generally integrate into our websites. One of them is the "Load More" feature which we can see on every second website we visit. The load more feature is used to load more or show more content available on the webpage to the visitor. Initially, half of the content is hidden, only some part of it i
4 min read
How to load data from JavaScript array using jQuery DataTables plugin ?
DataTables are a modern jQuery plugin for adding interactive and advanced controls to HTML tables for our webpages. It is a simple-to-use plug-in with a huge range of options for the developer's custom changes. The common features of the DataTable plugin are paging, multiple column ordering, sorting, and searching. The pre-compiled files which are
2 min read
JavaScript Atomics load() Method
Among the Atomic Operations, there is an inbuilt operation Atomics.load() that is used to return a value that is residing at a given position in an array. The integer typedarray and the index of the value are passed as an argument to the function. Atomics.load() returns the value at the given position of the array. Note: that Atomics.load() can be
3 min read
HTML | DOM Audio load() Method
The Audio load() method is used for reload the audio element. The Audio load() method is used to update the audio element after changing the source or other settings. The Audio load() method does not accept any parameters and does not return any values.Syntax audio.load() Example: C/C++ Code <!DOCTYPE html> <html> <body style="t
1 min read
HTML DOM Video load( ) Method
The Video load() method is used for re-loading the video element, updating it after the source or setting changes. It ensures changes take effect and the video reflects updated configurations. Syntax: videoObject.load()HTML DOM Video load( ) Method ExamplesExample: In this example, we display a video with two sources. When the "Change Source" butto
1 min read
How to load notification alert on top right corner without click of button in bootstrap ?
Bootstrap Toast component provides an easy way to mimic the functionality of push notifications. Bootstrap Toast is like an alert box that can be displayed to the user whenever an event occurs. This component has been built with CSS flexbox which makes it easy to position and align. It is not necessary to show the toast on a button click. It can be
2 min read
PHP | DOMDocument load() Function
The DOMDocument::load() function is an inbuilt function in PHP which is used to load an XML document from a file. Syntax: mixed DOMDocument::load( string $filename, int $options = 0 ) Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter holds the path to the XML document. $options: This
1 min read
How to tell if a script tag failed to load?
The problem is to identify whether the passed script loaded successfully or not using JavaScript. There are two methods which are discussed below Approach 1: Set a variable loaded = false.Pass the URL of the JavaScript file in the <script> tag.Set the onload parameter, if the script loaded set loaded = true. Example: This example illustrates
2 min read
How to execute AngularJS controller function on page load ?
In this article, we will see how to execute/call a JS function on page load using AngularJS. This function can be used to perform initialization. Calling a function or initializing a single value on page load in AngularJS is quite easy. AngularJS provides us with a dedicated directive for this specific task. It's the ng-init directive. Syntax: <
2 min read
How to load font-awesome using SCSS ?
In order to load font-awesome icons in SCSS you need to follow these steps: Install Font-Awesome starter kit: Go to the font-awesome website and download the free starter-kit for web. Create a project in your directory: In your project directory create 2 folders: public resources The public folder will contain your html and css files whereas the re
2 min read
How to load the contents of a text file into a JavaScript variable?
In this article, we will examine how to read the contents of any text file that exists on your computer into a variable using JavaScript. The following are a few basic pointers that everybody should brush through before looking at the code: Event listeners: These are predefined functions that exist in JavaScript. They take two arguments, the first
3 min read
How to load data from JSON into a Bootstrap Table?
This article describes how a Bootstrap Table is created using a given JSON data. The data can be retrieved by either importing it or representing it in JavaScript. The following are given two methods to do it. Displaying JSON data without importing any file: The JSON file that has to be read can be represented using JavaScript. This can then be giv
4 min read
What Happens To CSS When We Load a Page?
Although we know how to write the code of the HTML and CSS many developers don't know how the HTML and CSS will be processed by the browser. If you don't know how the HTML and CSS will be processed then don't worry I am here to help you. This article will help aspiring developers and developers working with web basics to solidify the concepts that
3 min read
How to get focused a button automatically when the page load using HTML ?
The <button> tag in HTML is used to define the clickable button. The <button> tag is used to submit the content. The images and text content can use inside <button> tag. The <button> autofocus attribute is used to get focused button automatically after loading the web pages. Syntax: <button type="" autofocus> Submit
1 min read
How to load window width value to SCSS variable ?
The task is to load the window width value to an SCSS variable and use it. Let us take a brief introduction to an SCSS. SCSS Variables can store any kind of value color, font-family, font-size, clip-path values, etc, and are mainly used to maintain the reusability of code across the stylesheets. Approach: Step 1: Define a function in SCSS and gener
2 min read
How to set focus on input field automatically on page load in AngularJS ?
We can focus on any input field automatically using the angular directives. Here we create a custom directive that can auto-focus on any field in the form. Creating a custom directive is just like creating an Angular component. To create a custom directive we have to replace @Component decorator with @Directive decorator. Approach : Create the Angu
3 min read
How to load CSS and JS files dynamically ?
Generally, the CSS and JS files are included statically with HTML code. It means they are written in the script or the link tag in the HTML code. But this slows down the execution as a bulk of code is loaded unnecessarily. It may or may not use the functionality related to that DOM element. So dynamically, we load the CSS and JS files during the ru
5 min read
How to load multiple helper files in CodeIgniter framework ?
A helper is considered to be a collection of functions that are aligned under a single particular category. Helpers are easily available in the CodeIgniter framework. They basically deal with procedural functions. It is used to ease up the tasks that are to be performed. The helpers can be loaded in the controllers to facilitate the task operation.
2 min read
How to load classes in PHP ?
PHP load classes are used for declaring its object etc. in object oriented applications. PHP parser loads it automatically, if it is registered with spl_autoload_register() function. PHP parser gets the least chance to load class/interface before emitting an error. Syntax: spl_autoload_register(function ($class_name) { include $class_name . '.php';
2 min read
Load Balancing Flask Application using Nginx and Docker
Load balancing means efficiently distributing the incoming traffic to different server instances. Nginx is open-source software that can be used to apply load balancing to backend systems. Nginx also can be serve services such as reverse proxy, caching, web server, etc. Docker is a tool that gives a virtualization solution for a complete software p
5 min read
three90RightbarBannerImg