How to load another .js file in a .js file?

Poster
Posted by Poster under JavaScript category on | Views : 10157
Lets say you have a .js file and you want another .js file to load from this .js file without addding another <script> tag to load .js file in every page of the website, you can use following function

function addJavascript(jsname) {
var th = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',jsname);
th.appendChild(s);
}


You can call above function like this
addJavascript('/yourjsfile.js');

Comments or Responses

Login to post response