Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    France, Bordeaux
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Include a javascript file in another javascript

    I have a Javascript library composed of a dozen of .js files.
    I want to prevent the HTML file from being polluted with many html <script> elements, one for each of the inclusions.

    Is there a way for a javascript to include an external javascript file?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MyttO
    Is there a way for a javascript to include an external javascript file?
    just stick to HTML.

    Code:
    <script src="myjsfile.js"></script>
    *keep it simple (TM)

  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. Do it with document.write.
    CATdude about IE6: "All your box-model are belong to us"

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    call one Js file and using php, or some other SS lang to include all the other js files into that?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    Canada, eh?
    Posts
    133
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Straight-forward answer:


    Include one JS file that will contain all the other scripts:

    Code:
    <script type="text/javascript" src="thejsfile.js"></script>
    And in thejsfile.js:

    Code:
    document.write('<script type="text/javascript" src="file1.js"></script>');
    document.write('<script type="text/javascript" src="file2.js"></script>');
    document.write('<script type="text/javascript" src="file3.js"></script>');
    document.write('<script type="text/javascript" src="file4.js"></script>');
    document.write('<script type="text/javascript" src="file5.js"></script>');
    ... etc.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    function includeJS(jsPath){
      var script = document.createElement("script");
      script.setAttribute("type", "text/javascript");
      script.setAttribute("src", jsPath);
      document.getElementsByTagName("head")[0].appendChild(script);
    }

  • #7
    Registered User
    Join Date
    Dec 2004
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What does the above code do and how do I use it?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    It dynamically adds external scripts through DOM. A more elegant way than using document.write. You just call the includeJS() function passing the path to the external script.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •