Attempting nested javascript include

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brit

    Attempting nested javascript include

    Hello,
    I have been trying to include a js file from within another js file. I
    can get this to work for firefox, but not i.e.

    What I have is
    a.html ...
    <script language='JavaS cript'
    src='b.js'></script><script> Function();</script>
    b.js ....
    function Function() {
    var s1 = "";
    document.write( "<sc" + s1+ "ript language='JavaS cript'
    src='c.js'></sc" + s1 + "ript>");
    document.write( "<sc" + s1+ "ript language='JavaS cript'>Func2(); </sc" +

    s1 + "ript>");
    }
    and then c.js
    I tried the s1="" from another googled post - no joy.
    if I change a.html to
    <script language='JavaS cript' src='cb.js'></script>
    <script language='JavaS cript'
    src='b.js'></script><script> Function();</script>
    it works.

    Is anyone able to get this to work with IE 6?

    Thanks - Bryan.

  • Vic Sowers

    #2
    Re: Attempting nested javascript include


    "Brit" <monty@english. net> wrote in message
    news:1116605282 .069454.262770@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > Hello,
    > I have been trying to include a js file from within another js file. I
    > can get this to work for firefox, but not i.e.
    >
    > What I have is
    > a.html ...
    > <script language='JavaS cript'
    > src='b.js'></script><script> Function();</script>
    > b.js ....
    > function Function() {
    > var s1 = "";
    > document.write( "<sc" + s1+ "ript language='JavaS cript'
    > src='c.js'></sc" + s1 + "ript>");
    > document.write( "<sc" + s1+ "ript language='JavaS cript'>Func2(); </sc" +
    >
    > s1 + "ript>");
    > }
    > and then c.js
    > I tried the s1="" from another googled post - no joy.
    > if I change a.html to
    > <script language='JavaS cript' src='cb.js'></script>
    > <script language='JavaS cript'
    > src='b.js'></script><script> Function();</script>
    > it works.
    >
    > Is anyone able to get this to work with IE 6?
    >
    > Thanks - Bryan.
    >[/color]

    Here's what I use (I have this in a 'util.js' file that I load 'manually'
    (with a <script src=...> element):
    =============== =====
    var Included={};
    function Include() {
    var src, url, heads;
    heads = document.getEle mentsByTagName( "head");
    if (heads.length== 0) {throw new Error(0x8009000 ,"Include: No <head> tag
    exists. Scripts not loaded.");}
    url = document.URLUne ncoded;
    for (a=0; a<arguments.len gth; a++) {
    src = arguments[a];
    if (Included[src]) continue;
    Included[src] = true;
    if (url.substr(0,5 )=="file:") {
    if (src.charAt(0)= ="/") src = "C:<your webroot directory full path>"+src;
    else src = url.slice(7,url .lastIndexOf("\ \")+1)+src;
    }
    heads[0].appendChild(do cument.createEl ement("<script language='JavaS cript'
    src='"+src+"' />"));
    }
    }
    =============== =====
    Then in the '<head>' I put:

    <script
    language="javas cript">Include( "/_ScriptLibrary/MyScript.js","L ocalScripts.js" ,...)</script>

    You may also use Include() in other 'Included' or manually loaded script
    files, but may not reference their contents until the parent file is fully
    loaded.

    This works fine in IE6 but is untested in any other enviromant.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Attempting nested javascript include

      "Brit" <monty@english. net> writes:
      [color=blue]
      > I have been trying to include a js file from within another js file. I
      > can get this to work for firefox, but not i.e.[/color]

      *how* does it not work in IE? Does it give an error message? Do you
      have Javascript error messages enabled?
      [color=blue]
      > <script language='JavaS cript'
      > src='b.js'></script>[/color]

      Should be
      <script type="text/javascript" src="b.js"></script>
      The "type" attribute is mandatory, and the language attribute is
      deprecated and not needed.
      [color=blue]
      > <script>Functio n();</script>[/color]

      ....
      [color=blue]
      > function Function() {[/color]

      *Bad* name for a function. There is already a built-in function called
      Function.
      [color=blue]
      > var s1 = "";
      > document.write( "<sc" + s1+ "ript language='JavaS cript'
      > src='c.js'></sc" + s1 + "ript>");[/color]

      No need for being this convoluted. Just write:
      document.write( "<script type='text/javascript' src='c.js'></script>");

      Since this is inside a javascript file, and not inside an HTML file,
      there is no need to escape anything.

      If this had been inside a script tag in an HTML file, all you need to do
      is change all "</" to "<\/", i.e.,
      document.write( "<script type='text/javascript' src='c.js'><\/script>");

      Your attempt at escaping by putting "s1" in the middle would still
      fail in a fully standards compliant HTML parser, because such one
      would end the script tag at the first occurence of "</". Actual
      browsers only end it at "</script", but there is no need to take
      chances when being correct is so easy.
      [color=blue]
      > document.write( "<sc" + s1+ "ript language='JavaS cript'>Func2(); </sc" +
      >
      > s1 + "ript>");
      > }
      > and then c.js[/color]

      .... defining Func2, I guess.
      [color=blue]
      > I tried the s1="" from another googled post - no joy.[/color]

      And no need.
      [color=blue]
      > if I change a.html to
      > <script language='JavaS cript' src='cb.js'></script>
      > <script language='JavaS cript'
      > src='b.js'></script><script> Function();</script>
      > it works.
      >
      > Is anyone able to get this to work with IE 6?[/color]

      Again, what error message does it give you?

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Brit

        #4
        Re: Attempting nested javascript include

        Thanks for the response. I stripped down the code and renamed the
        functions - supposedly for clarity.

        Anyway, I tried with the added type field, however that didn't make any
        difference here. The error I get is 'Object Expected' with a code of 0.
        The line it points to is the call to the function in b.js. If I remove
        the call to Func2 in b.js, it works fine. If I replace the call to
        Func2 in b.js, but include c.js in a.html it works. This is what I've
        done for now - but it's annoying that it doesn't work with just
        including b.js and calling Function.

        Comment

        Working...