[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reference external Javascript file from external javascript file - How?

Posted on 2009-02-11
4
Medium Priority
?
611 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.
0
Comment
Question by:afflik1923
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:gam3r_3xtr3m3
gam3r_3xtr3m3 earned 800 total points
ID: 23617538
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

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23617594
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

0
 
LVL 16

Accepted Solution

by:
sunithnair earned 1200 total points
ID: 23617603
Or you can actually put in the html page refer the basesrc.js first and then the childsrc.js.
0
 

Author Closing Comment

by:afflik1923
ID: 31545852
All suggestions good but the easiet and one I used was the omarked as accepted solution.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question