We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Reference external Javascript file from external javascript file - How?

afflik1923
afflik1923 asked
on
Medium Priority
632 Views
Last Modified: 2012-05-06
It should be simple but I must be tired or crazy right now.
But how do I reference a javascript file from another javascript file.

In my particular case I need to import a JSON co library into a javascript file that is called from the main HTML page.
But that does not matter, I just need to know how to call a javascript file from another javacript file. (I of course know how to reference one from a HTML file).

Equivelant command in PHP would be require_once.

Thanks.
Comment
Watch Question

you can do it like this method below.

Hope that helps,

Andrew


// load external script
 
var filename = "script.js";
var scr = document.createElement('script');
scr.setAttribute("type","text/javascript");
scr.setAttribute("src", filename);
document.getElementsByTagName("head")[0].appendChild(scr);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
I dont think it is possible. Try this anyway inside the js file that in the child js file
document.write('<script type="text/javascript" src="basesrc.js"><\/script>');
 
or
 
var script = document.createElement("script");
script.setAttribute("type","text/javascript");
script.setAttribute("src", "basesrc.js");
document.getElementsByTagName("head")[0].appendChild(script);

Open in new window

Or you can actually put in the html page refer the basesrc.js first and then the childsrc.js.

Author

Commented:
All suggestions good but the easiet and one I used was the omarked as accepted solution.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.