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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    include JS files in other files?

    forgive my ignorance... but in a JS file eg: "matt.js" how do I include other .js files....

    at the mo they are all in the main HTML window and there are about 10 JS files.... but in the HTML I'd like to only include one.... is that possible??

    thanks...

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "thefile.js");
    document.getElementsByTagName("head")[0].appendChild(script);

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    woah... cool thanks..!! glad I asked now - I was going to just put "include ....." !!

    do I have to do all that for every file or can I do something like:

    Code:
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    
    script.setAttribute("src", "thefile.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    
    script.setAttribute("src", "thefile2.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    etc....

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    or just

    document.getElementsByTagName("head")[0].appendChild("script.js");
    document.getElementsByTagName("head")[0].appendChild("script2.js");

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by homerUK
    or just

    document.getElementsByTagName("head")[0].appendChild("script.js");
    document.getElementsByTagName("head")[0].appendChild("script2.js");
    Definitely not. appendChild method expects an element node not a string.
    About the other way, not sure, you have to try it.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by homerUK
    woah... cool thanks..!! glad I asked now - I was going to just put "include ....." !!

    do I have to do all that for every file or can I do something like:

    Code:
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    
    script.setAttribute("src", "thefile.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    
    script.setAttribute("src", "thefile2.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    etc....
    I tried it and it didn't work. You have to create a separate variable for each script.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    yeah, just found out myself as well!

    cheers for the help - much appreciated!

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    hmm....i tried the following and it didnt work

    Code:
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "path/to/file.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", "another/path/this.js");
    document.getElementsByTagName("head")[0].appendChild(script);
    is it possibly the paths? Should they be relative to the calling page?

    eg the html calls "main.js" and the other JS files are scattered around other folders.... should the paths be relative to the main.js or the HTML page?

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You should use different variables for the script.

    var script = document.createElement("script");
    ...
    var script2 = document.createElement("script");

    You can store the head reference to a variable and then reuse it in each addition of script.

    var head = document.getElementsByTagName("head")[0];
    ...
    head.appendChild(script);
    ...
    head.appendChild(script2);


    Since that code is in external js, the path should be relative to the js.


  •  

    Posting Permissions

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