How to include JavaScript file into another

The old versions of Client-side JavaScript offers no built in functions to manage multiple scripts within the browser. But, it is possible to dynamically generate a JavaScript tag and append it to HTML document from inside other JavaScript code. JavaScript file into another example Let's suppose you've two JavaScript files named "message.js" and "next.js" and you included only the message.js in the HTML file. If you want to call a function from next.js , you can dynamically generate a JavaScript tag and append it to HTML document from inside other JavaScript code. Javascript File (message.js)
function msg(){ alert("Import File"); }
Javascript File (next.js)
function msg1(){ alert("Included file"); }
HTML File
<html> <head> <script type="text/javascript" src="message.js"></script> <script> function includeFile(incFile) { var e=window.document.createElement('script'); e.setAttribute('src',incFile); window.document.body.appendChild(e); } </script> </head> <body onLoad="includeFile('next.js');"> <p>Include a JavaScript file in another JavaScript file</p> <form> <input type="button" value="Import File" onclick="msg()"/> <input type="button" value="Includ File" onclick="msg1()"/> </form> </body> </html>
Here you can see the msg1() function is declared in next.js and not included in HTML file . But with the help of the Javascript function "includeFile(incFile)" you can dynamically generate a JavaScript tag and append next.js to HTML document from inside other JavaScript code.