Click to See Complete Forum and Search --> : Is there a #include for javascript?


Martin O
February 14th, 2006, 12:32 PM
In c I can use #include"somefile.h" to include a file. Is there a way to do this in a .js file?

PeejAvery
February 14th, 2006, 01:35 PM
In c I can use #include"somefile.h" to include a file. Is there a way to do this in a .js file?
This question was just asked. In fact there is only one way to do it but it did not help the other person. Maybe it will help you. Here (http://www.source-code.biz/snippets/vbscript/5.htm) is the link.

olivthill
February 14th, 2006, 03:51 PM
Yes, this question is often asked, but I'm afraid your link, Peejavery, is for VBScript and Martin O's question is for JavaScript.

You can have
<script src="foo.js" type=text/javascript></script>

or

<object id="foo" data="foo.js" type="text/javascript"></object>

PeejAvery
February 14th, 2006, 04:06 PM
...Martin O's question is for JavaScript.
You are right. I read right over that since I just answered the question for VBScript yesterday. Thanks for keeping me on my feet.

Asok
February 14th, 2006, 04:16 PM
I think I understand what you are asking...

You want to include a set of instructions from one .js file into anther .js file.

Is this .js file you are importing TO used within a web page? If so, you just have to include the file above the other.

Example:

<head>
<script language='JavaScript' src='jsIncludeFile.js'></script>
<script language='JavaScript' src='jsMainFile.js'></script>
</head>


If you are within a .js file and intend to run it as a stand alone script, you will have a harder time, but I believe it is still possible.

I haven't tested this, so beware...

Try creating a SMALL include file (testInclude.js).

1. Read the contents of the testInclude.js file into a string. (Ensure all semi-colons are used!!!!)
2. Pass this value into the eval statement
-> Usage: eval( string expression).

I do not know how this will work with functions. May work, may not. Good luck!!!