
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to load a JavaScript file Dynamically?
Following is the code to load a JavaScript file dynamically −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Load a JavaScript file Dynamically</h1> <div style="color: green;" class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click the above button to load external JavaScript </h3> <script> let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { var jsRef = document.createElement("script"); jsRef.setAttribute("src", "sample.js"); document.getElementsByTagName("head")[0].appendChild(jsRef); }); </script> </body> </html>
script.js
resEle.innerHTML='JavaScript has been loaded';
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to dynamically load a Python class?
- Load a text file and find number of characters in the file - JavaScript
- How to load a .ttf file in Matplotlib using mpl.rcParams?
- How to create and store property file dynamically in Java?
- How to load an HTML file into the canvas?
- How to set JavaScript object values dynamically?
- How to use jQuery.getJSON to load JSON file in jQuery?
- How to load a file into the JShell session in Java 9?
- How to change variables in the .env file dynamically in Laravel?
- How to add HTML elements dynamically using JavaScript?
- How to load a JavaScript function using the variable name?
- How to use JavaScript to load a webpage after 5 seconds?
- How to merge properties of two JavaScript Objects dynamically?
- How to dynamically combine all provided arrays using JavaScript?
- How to Add Commas Between a List of Items Dynamically in JavaScript?

Advertisements