Skip to main content

Archive

Show more

How to include one javascript file in another

how-to-include-one-javascript-file-in-another


Question: How to include one javascript file in another

Answer:

There are various methods to add one javascript file into another javascript file.


First:

document.writeln("<script src='script.js' type='text/javascript'></script>");


Second: This method is similar to first method but here we are creating all element with javascritp code.

function include(file) {  
  var script  = document.createElement('script');
  script.src  = file;
  script.type = 'text/javascript';
  script.defer = true;  
  document.getElementsByTagName('head').item(0).appendChild(script);
}

You can use import, export concept of javascript to include one javascript file variable in other javascript file.



We try to provide you the best content, if there is any mistake in this article or there is any mistake in code, then let us know.

Comments