How to include JavaScript file into another JavaScript file

By | August 8, 2020

Before 2015, when the ES6 module was not introduced in JavaScript, there was no functionality available to import, include any file. However, when ES6 module introduced, we can import, include, and require files in JavaScript very easily with some lines of code.

In this tutorial, we are going to include a JavaScript file into another JavaScript file with document.createElement() function. Here we go.

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>Include javascript filetitle> 
      
    <script type="text/javascript"> 
        var script = document.createElement('script'); 
          
        script.src = "PATH TO FILE/javascriptfile.js"; /// this will be the file path which you want include in this page
          
        document.head.appendChild(script) 
    script> 
head> 
  
<body> 
<b>Include JavaScript file into another JavaScript fileb>
body> 
  
html> 

images

Leave a Reply

Your email address will not be published. Required fields are marked *