Possible Duplicate:
How to include a JavaScript file in another JavaScript file?
I want to include a JavaScript file in a JavaScript file. include('filename.js');
is not working
What is the right code?
-
@zod is it includ or include, either way not workingX10nD– X10nD2010年10月21日 20:13:55 +00:00Commented Oct 21, 2010 at 20:13
-
1there is no single line include statement in JSPekka– Pekka2010年10月21日 20:23:22 +00:00Commented Oct 21, 2010 at 20:23
4 Answers 4
function includeJS(incFile)
{
document.write('<script type="text/javascript" src="'+ incFile+ '"></script>');
}
Then include a second JavaScript file by calling:
includeJS('filename.js');
6 Comments
</script>
ending an inline script, <\/script>
is much more readable (and marginally faster).Use:
<script language="javascript" src="first.js"></script>
<script language="javascript" src="second.js"></script>
You can access the variables from the first file in the second file.
There isn't any need to include one JavaScript file into another. JavaScript code is globalised. You can include both the files in the HTML/JSP page.
3 Comments
Use document.write
in the first JavaScript function:
document.write('<scr'+'ipt type="text/javascript" src="filename.js" ></scr'+'ipt>');
2 Comments
If you do the document.write method bear in mind that the code within the file will not be guaranteed to be loaded once document.write returns.
You may want to have some type of callback mechanism when the included file has loaded. That is, register a callback before document.write, and at the very end of your javascript file make a call to the callback function.