Reference external Javascript file from external javascript file - How?

Posted on 2009-02-11
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.

Question by:afflik1923
    LVL 7

    Assisted Solution

    you can do it like this method below.

    Hope that helps,


    // load external script
    var filename = "script.js";
    var scr = document.createElement('script');
    scr.setAttribute("src", filename);

    Open in new window

    LVL 16

    Expert Comment

    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>');
    var script = document.createElement("script");
    script.setAttribute("src", "basesrc.js");

    Open in new window

    LVL 16

    Accepted Solution

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

    Author Closing Comment

    All suggestions good but the easiet and one I used was the omarked as accepted solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    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…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now