Page 1 of 1

Import a js file into a js file

Posted: 2007-05-27 12:34pm
by Lord MJ
I'm working on a Javascript file and I was wondering how do I include another .js file in it?

Or do I even have to, if I include 2 .js files in the same html page would functions in js1 be able to recognize functions defined in .js2?

Posted: 2007-05-28 12:48am
by Hugh
Javascript has no native import/include statement. So yes, the only way is to <script> both files into the same HTML - they will share one namespace.

Re: Import a js file into a js file

Posted: 2007-05-28 02:59am
by Chris OFarrell
Lord MJ wrote:I'm working on a Javascript file and I was wondering how do I include another .js file in it?

Or do I even have to, if I include 2 .js files in the same html page would functions in js1 be able to recognize functions defined in .js2?
I'm preaty sure you can't include one Javascript file inside another Javascript file. Sun designed JS for small menial stuff like input validation and what not, though its grown somewhat over the years. The idea being that hardcore programming stuff would all be done with the glorious JSP technology which would quickly replace all over client/server technology!

*snickers*

But on topic, if you include the two scripts in the HTML page, they are in the same namespace as a matter of course.

Posted: 2007-05-28 04:37am
by Hugh
There is, of course, the option of using Javascript to inject <script src="..."> tags into the document tree, which will effectively simulate your hypothetical insert() statement. But IMO it's not worth the fuss, unless you need to conditionally load code, or something.