Welcome to WebmasterWorld Guest from 54.204.165.156

Forum Moderators: open

JAVASCRIPT - Linking javascipt files in other javascript files

   
2:32 am on Apr 4, 2003 (gmt 0)

10+ Year Member



I use IE5. And Javascript. In a javscript file e.g. abc.js is it possible to include another javascript file? so the other files functions can also be used?... like a libray routine.

... a but like C languages #INCLUDE etc...

3:11 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Not that I know of.

>>I use IE5
Are you creating a desktop application?

3:33 am on Apr 4, 2003 (gmt 0)

10+ Year Member



No
3:38 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Is this going to go on the web?
3:46 am on Apr 4, 2003 (gmt 0)

10+ Year Member



yes
3:58 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can get a similar effect to a #include by including the javascript files in your html file. For example, with the following in your html file:

<script language="JavaScript" src="scripts/file1.js"></script>
<script language="JavaScript" src="scripts/file2.js"></script>

If you define a variable, function or whatever in file1.js, you can call your function, or reference your variable from code in file2.js

Shawn

3:58 am on Apr 4, 2003 (gmt 0)

10+ Year Member



yeah, sure.

Two things come to mind:

First, the obvious - load two (or more) .js files with the document and code from one file can reference code in the other file.
But both files must be linked to the document.

More in line with what I think you're looking to do is to link to a scripted page which returns javascript and can contain an include just like any other scripted page.
Details:

<script type="text/javascript" src="mainscript.asp"></script>

In the mainscript.asp page you set the response.contenttype to "text/javascript" so that it returns data with the proper type.
From there on in, you can include code to your heart's content just like any other asp page. It's just got to be valid, working javascript.

This can be done with any sophisticated server-side scripting engine. PHP for sure.

4:23 am on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A nasty cludge is to make the javascript write out the includes it needs as html... eg.

includefile = 'somefile determined dynamically';
document.write('<script type='text/javascript src="'+includefile+'"></script>');

nasty - I've used this on IE 5.5 only - not sure if it will work on other browsers.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month