Nested includes

Discussion in 'JavaScript' started by James McMurray, Nov 9, 2007.

  1. #1
    Is it possible to do something like the following:

    index.html:
    <head>
    <script src="a.js"></script>
    </head>
    Code (markup):
    a.js
    <script src="b.js"></script>
    
    functionThatRequiresB();
    Code (markup):
    b.js
    code that lets the prior function work
    Code (markup):

     
    James McMurray, Nov 9, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i'm not sure if that's possible.. but i'm pretty sure this will do the trick

    <head>
    <script src="a.js"></script>
    <script src="b.js"></script>
    </head>
     
    Jamie18, Nov 9, 2007 IP
  3. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what is the point in all of this
     
    sin0cide, Nov 9, 2007 IP
  4. James McMurray

    James McMurray Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry for the necro. I thought I had email notification turned on, and assumed nobody had responded to the thread, so just saw this.

    I want scripts to be able to access libraries themselves without having to include all of it's prerequisite files externally, sort of like using the "#include" directive inside a C header file to include another header file. You free the end users and maintainers from needing to fully understand your toolset in order to use it.

    Basically I want to make my libraries more user friendly by using:

    <script src="mylibrary.js"></script>

    instead of

    <script src="prereq1.js"></script>
    <script src="prereq2.js"></script>
    <script src="mylibrary.js"></script>
     
    James McMurray, Nov 24, 2007 IP
  5. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #5
    I think this's illegal :D
     
    temp2, Nov 25, 2007 IP
  6. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hogan_h, Nov 25, 2007 IP
  7. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #7
    Are you asking for import a javascript from other javascript (inside or outside your html) ?
    If yes, you can use:

    
    document.write("<script type='text/javascript' src='test.js'></scr"+"ipt>");
    
    Code (markup):
    Note that you should split the closing script tag into two parts.

    Another option is using the DOM model:
    
    // Create the Script Object
    var script = document.createElement('script');
    script.src = 'server.js';
    script.type = 'text/javascript';
    script.defer = true;
    script.id = 'scriptID'; // This will help us in referencing the object later for removal
    // Insert the created object to the html head element
    var head = document.getElementsByTagName('head').item(0);
    head.appendChild(script);
    
    Code (markup):
    And to remove that object:
    
    var head = document.getElementsByTagName('head').item(0);
    var old = document.getElementById('scriptID');
    if (old) head.removeChild(old);
    
    Code (markup):
     
    ajsa52, Nov 25, 2007 IP
  8. James McMurray

    James McMurray Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks everyone!

    I don't doubt it, but I'm curious as to what standard and/or bug makes it that way.
     
    James McMurray, Nov 25, 2007 IP
  9. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #9
    The first string "</javascript>" found will mark the end of javascript code. So that's to avoid that thing, because your javascript code does not end there.
     
    ajsa52, Nov 25, 2007 IP
  10. James McMurray

    James McMurray Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Makes perfect sense. Thanks!
     
    James McMurray, Nov 26, 2007 IP