How to call html file in javascript

There are many ways to call a JavaScript function in the HTML document, and it is also not a difficult task. First, we have used one of the easiest ways to call a JavaScript function in HTML document:

In this method, we will create and define a function in the HTML document's head section. To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.

To understand it more clearly let's see the given program:

Program

Explanation of program

In the above-given program, we have created a simple HTML document. Inside the head section of the HTML document, we have defined a function (e.g myfunction();) inside the script tags .

On the other hand, inside the body section, we displayed some text and created a button. To call our function, we have used the onclick attribute along with the button and when the user clicks on that button our function gets executes and display an alert message, as you can see in the output.

Output

How to call html file in javascript

Calling a function using external JavaScript file

We can also call JavaScript functions using an external JavaScript file attached to our HTML document. To do this, first we have to create a JavaScript file and define our function in it and save itwith (.Js) extension.

Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag . Because we have stored our HTML document and JavaScript file in the same folder, we have just named our JavaScript file in the "scr" attribute instead of providing the full path in the head section.

Inside the body section, we displayed some text and created a button. To call our function, we have used the onclick attribute along with the button and when the user clicks on that button our function gets executes and display an alert message, as you can see in the output.

Output

How to call html file in javascript

Now click on the given button:

How to call html file in javascript


Learn how to include HTML snippets in HTML.


The HTML

Save the HTML you want to include in an .html file:

content.html

Google Maps

Animated Buttons

Modal Boxes

Animations

Progress Bars

Hover Dropdowns

Click Dropdowns

Responsive Tables


Include the HTML

Including HTML is done by using a w3-include-html attribute:

Example

w3-include-html="content.html">


Add the JavaScript

HTML includes are done by JavaScript.

Example

Call includeHTML() at the bottom of the page:



Include Many HTML Snippets

You can include any number of HTML snippets:

Example


Try it Yourself »


How do I reference an HTML file using JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the tags in your HTML document.

Can you put HTML in JavaScript?

Yes, we can also set the HTML/CSS properties in Javascript too. Create a new text node. Create a new HTML element. Get or set the id of an element.

How do you load a file in JavaScript?

To load a JavaScript file dynamically: Create a script element..
src : the file path..
type : file type - "text/javascript".
async : if we set async to false , then the file will be loaded and executed first before proceeding to the next action..

How can I call HTML file in HTML?

How TO - Include HTML.
The HTML. Save the HTML you want to include in an .html file: content.html. ... .
Include the HTML. Including HTML is done by using a w3-include-html attribute: Example. ... .
Add the JavaScript. HTML includes are done by JavaScript. Example. ... .
Include Many HTML Snippets. You can include any number of HTML snippets:.