How to include a .js file (a JavaScript file) within another .js file

Actually, there is no function you can call to include a JavaScript file within another one. This is the bad news. The good news is that you can use a trick to accomplish this task. Suppose you have two JavaScript files, say file1.js and file2.js. You want to include file1.js within file2.js because you want to use file1′s functions. Here is the code you can use:

//this is file2.js

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

// here you can use functions defined in file1.js

As you can see it suffices “to write” in the browser the code you generally use to include a JavaScript file within an HTML file and you’re done!

This entry was posted in IT, Programming and tagged . Bookmark the permalink.

Leave a Reply

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

*

* Copy this password:

* Type or paste password here:

3,535 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>