0

HI,

I have a javascript lib which has a.js,b.js,c.js files....My logic is in d.js...How do I refer to a.js,b.js,c.js files in d.js???

Is it possible to refer??

I tried using

document.write('<script type="text/javascript" src="a.js"></script>');

and

<script type="text/javascript" src="a.js"></script>

I get error telling the variables are not defined and the functions(they are in external javascript files) are not defined

Thanks:)

mplungjan
180k29 gold badges183 silver badges246 bronze badges
asked Mar 8, 2011 at 9:15
1
  • 2
    Not enough information. And PLEASE write normal English. This is not an SMS service. document.write should work, you must however escape the end tag: <\/script> Commented Mar 8, 2011 at 9:17

4 Answers 4

2

you can dynamically load Javascript files. This is a good link. http://ntt.cc/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html

However what I find more useful is to use PHP to render my javascript and in my php append all the javascript I need for a relevant page.

answered Mar 8, 2011 at 9:25
Sign up to request clarification or add additional context in comments.

Comments

1

yeah its possible to do it by using the DOM to add the js file to the head of the document.

 var headID = document.getElementsByTagName("head")[0];
 var newScript = document.createElement('script');
 newScript.type = 'text/javascript';
 newScript.language="Javascript";
 newScript.charset="ISO-8859-1";
 newScript.src = 'yourfile.js';

headID.appendChild(newScript);

Of course you then have to tell your current JS file that the one you added has loaded. I do that by declaring a JS function in the current JS file and then calling it at the end of the one that has been dynamically loaded

answered Mar 8, 2011 at 9:23

4 Comments

Problem is, you should halt code execution in d.js until a.js, b.js and c.js are loaded.
I've updated the answer with a way to tell the script the new one has loaded
Why not just use script loader which is already tested across different browsers, and has that features that you describe.
I'm not allowed to use third party tools like this in my place of work without going through crap.
0

Try this js loader http://www.dustindiaz.com/scriptjs/ I find it very convenient. It has feature to manage dependencies, so you can give rules in which order scripts load.

answered Mar 8, 2011 at 9:24

Comments

0

I would suggest merging all required files into a lib.js file. In addition to not causing problems with undefined variables, it also reduces the HTTP requests to 1 from 4.

Try using the closure compiler to merge your code into a single file.

answered Mar 8, 2011 at 9:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.