...

View Full Version : include JS files in other files?



homerUK
06-25-2004, 12:14 PM
forgive my ignorance... but in a JS file eg: "matt.js" how do I include other .js files....

at the mo they are all in the main HTML window and there are about 10 JS files.... but in the HTML I'd like to only include one.... is that possible??

thanks...

glenngv
06-25-2004, 12:20 PM
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "thefile.js");
document.getElementsByTagName("head")[0].appendChild(script);

homerUK
06-25-2004, 12:29 PM
woah... cool thanks..!! glad I asked now - I was going to just put "include ....." !!

do I have to do all that for every file or can I do something like:



var script = document.createElement("script");
script.setAttribute("type", "text/javascript");

script.setAttribute("src", "thefile.js");
document.getElementsByTagName("head")[0].appendChild(script);

script.setAttribute("src", "thefile2.js");
document.getElementsByTagName("head")[0].appendChild(script);

etc....

homerUK
06-25-2004, 12:29 PM
or just

document.getElementsByTagName("head")[0].appendChild("script.js");
document.getElementsByTagName("head")[0].appendChild("script2.js");

glenngv
06-25-2004, 12:35 PM
or just

document.getElementsByTagName("head")[0].appendChild("script.js");
document.getElementsByTagName("head")[0].appendChild("script2.js");
Definitely not. appendChild method expects an element node not a string.
About the other way, not sure, you have to try it.

glenngv
06-25-2004, 12:39 PM
woah... cool thanks..!! glad I asked now - I was going to just put "include ....." !!

do I have to do all that for every file or can I do something like:



var script = document.createElement("script");
script.setAttribute("type", "text/javascript");

script.setAttribute("src", "thefile.js");
document.getElementsByTagName("head")[0].appendChild(script);

script.setAttribute("src", "thefile2.js");
document.getElementsByTagName("head")[0].appendChild(script);

etc....
I tried it and it didn't work. You have to create a separate variable for each script.

homerUK
06-25-2004, 12:41 PM
yeah, just found out myself as well!

cheers for the help :thumbsup: - much appreciated!

homerUK
06-25-2004, 12:49 PM
hmm....i tried the following and it didnt work



var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "path/to/file.js");
document.getElementsByTagName("head")[0].appendChild(script);

var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "another/path/this.js");
document.getElementsByTagName("head")[0].appendChild(script);


is it possibly the paths? Should they be relative to the calling page?

eg the html calls "main.js" and the other JS files are scattered around other folders.... should the paths be relative to the main.js or the HTML page?

glenngv
06-28-2004, 03:36 AM
You should use different variables for the script.

var script = document.createElement("script");
...
var script2 = document.createElement("script");

You can store the head reference to a variable and then reuse it in each addition of script.

var head = document.getElementsByTagName("head")[0];
...
head.appendChild(script);
...
head.appendChild(script2);


Since that code is in external js, the path should be relative to the js.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum