16

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?

asked Oct 21, 2010 at 20:11
2
  • @zod is it includ or include, either way not working Commented Oct 21, 2010 at 20:13
  • 1
    there is no single line include statement in JS Commented Oct 21, 2010 at 20:23

4 Answers 4

9
function includeJS(incFile)
{
 document.write('<script type="text/javascript" src="'+ incFile+ '"></script>');
}

Then include a second JavaScript file by calling:

includeJS('filename.js');
Varun Sridharan
1,9882 gold badges21 silver badges56 bronze badges
answered Oct 21, 2010 at 20:13
Sign up to request clarification or add additional context in comments.

6 Comments

note that the call will return before the javascript file is loaded
This code will be placed in a js file not html or php, will it work out?
yes. it is specifically javascript in implementation
Now with attribution: A. B. Cryer.
Don't use string concatenation to avoid </script> ending an inline script, <\/script> is much more readable (and marginally faster).
|
7

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.

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
answered Oct 21, 2010 at 20:15

3 Comments

Unless for some reason, you want th JS to parse what the include file will be... not that likely, but does have application if you want to add some functionality to a page after the initial load...
may be .but can you point out a real time situation ?. i saw that cryer.co.uk , but i didnt try. exceptional cases are always there :)
Here's a case. I have an ASP page which dynamically determines, based on a database record, if it should load a js file. Which file is also a result of the database record. However, the code is designed to load only 1 js file. If that file has a dependency then I have to load it from javascript.
2

Use document.write in the first JavaScript function:

document.write('<scr'+'ipt type="text/javascript" src="filename.js" ></scr'+'ipt>'); 
Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
answered Oct 21, 2010 at 20:14

2 Comments

Why the downvotes? There are caveats, but the upvoted solution employs the same technique.
@Pekka, thanks for it. I know there are caveats but I think people should leave a comment why they are down voting an answer. After all that's how you learn from your mistakes..
1

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.

answered Oct 21, 2010 at 20:35

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.