PDA

View Full Version : Importing/Inheriting other external javascript files


MrBrett
12-05-2002, 01:38 AM
Hi

Bit of an advanced question :p ... if you are already inside an external .js file, is there a way to include another file. Basically what I need is for one file to *inherit* another. Any suggestions... I found you can do it the following way but that ends up running after the rest of the code in the first .js file.

document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='inheritedFile.js' TYPE='text/javascript'><\/SCR" + "IPT>");

Thanks in advance...

Later,
Brett

linuxdude
12-05-2002, 03:08 AM
whyy do you have that instead of just /
\/SCR" + "IPT>");

MrBrett
12-05-2002, 03:20 AM
Too Escape the character afaik. I copied that line from another script I found that did something similar. I didn't modify it coz it works... there could be a simpler version I guess but thats not important. What I need is to not use document.write to achieve this :p

glenngv
12-05-2002, 03:27 AM
You want somewhere in the first js to execute the 2nd js then after that, continue executing the first js?
If this is what you want, you should put the codes in the 2nd js in functions then just call these functions inside the 1st js.
That way you dont need to document.write to include it.
just this:

<head>
<script language="javascript" src="2nd.js"></script>
<script language="javascript" src="1st.js"></script>
</head>

MrBrett
12-05-2002, 06:55 AM
Not quite

plus I dont want to have to have two script tags you see. I have lots of small .js files that contain extensions to objects and regular functions I frequently use, then I have the script that I actually want to run on the page in which I would like to use the objects/functions from the previously mentioned scripts when needed. Basically I would like Class style inheritance... Similar to .Net's Namespace method, if possible:

// My Script

Import(myotherscript.js);

myFunctions()
{
// run small function from imported script
myImportedFunction();
}

glenngv
12-05-2002, 07:09 AM
Try if you can use import and export. I have never used them before. Here are the links:

http://www.devguru.com/Technologies/ecmascript/quickref/import.html
http://www.devguru.com/Technologies/ecmascript/quickref/export.html

Don't know if this applies to what you want to accomplish.

MrBrett
12-05-2002, 09:15 AM
Haha

that would be great if it worked like I wanted :)

Unfortunately it still requires the other script to be loaded somewhere beforehand (with a <script> tag) and also it seems as though it only works in Netscape 4.

Cheers anyway, got me excited :)

glenngv
12-05-2002, 10:01 AM
whatever way it is, the script SHOULD always be loaded in order to call the functions in it.

MrBrett
12-05-2002, 01:35 PM
Well thats really my question

can you load a script from another script?

Vladdy
12-05-2002, 02:02 PM
script = document.createObject('script');
script.type = 'text/javascript';
script.source = 'somejs.js';
document.getElementsByTagName('head')[0].appendChild(script);