Open In App

How to load external HTML file using jQuery ?

Last Updated : 15 Dec, 2022
Comments
Improve
Suggest changes
5 Likes
Like
Report

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 use the ready() function to ensure that our DOM is completely ready before doing any further tasks. We will load the external HTML using load() function.

Approach:

  • First, we will create our external HTML file.
  • Add a div element on the HTML file where we want to load external HTML.
  • Under the script, use the ready() function to check if DOM ready.
  • Then select the div element on which we want to load HTML using load().

External files: The following div-1.html and div-2.html files are used as external files.

div-1.html

div-2.html

HTML code: The following code demonstrates the loading of external files into an HTML div.

Output: 

Note: We are using the jQuery click() function, which means the external file will be loaded after we clicked on it. But if you want to load external files just after DOM is ready, just omit the click() event and call the load() function 


Next Article

Similar Reads