Javascript equivalent of #include or @import?

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

    #1

    Javascript equivalent of #include or @import?

    I know of course that you can use <script src=""></script> in an HTML
    document to include javascript code you don't want to spill all over the
    page.

    I'm wondering if there's a way to include other pieces of javascript
    code from *within* javascript.... a la #include in C, @import in Java,
    use/require in Perl, include()/require() in PHP, etc....

    Thanks,
    Weston

    ~==~

    weston8[at]cann8central.or g
    (remove eights to email me)

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Lasse Reichstein Nielsen

    #2
    Re: Javascript equivalent of #include or @import?

    Weston C <west8on[at]cann8central.Re moveEights.org> writes:
    [color=blue]
    > I know of course that you can use <script src=""></script> in an HTML
    > document to include javascript code you don't want to spill all over the
    > page.[/color]

    Not understood. Are you talking about
    <!-- <script src="..." type="text/javascript"></script> -->
    or
    <script src="..." type="text/javascript"><!-- --></script>
    ?
    The first does nothing, the second doesn't need the comment.
    [color=blue]
    > I'm wondering if there's a way to include other pieces of javascript
    > code from *within* javascript.... a la #include in C, @import in Java,
    > use/require in Perl, include()/require() in PHP, etc....[/color]

    Not in general, no. If you want to include it while the page is being
    loaded, you can use document.write to write a new Javascript tag:

    document.write( '<script type="text/javascript src="..."></script>');

    After the page has loaded, it becomes harder. Some browsers allow you
    to load a script by adding a new script tag to the page using DOM methods,
    or by changing the src attribute of an existing tag, or by changing the
    content of an existing script tag.
    Not all browsers understand all, or any, of these methods, though,
    so it's not something to depend on.

    /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

    • HikksNotAtHome

      #3
      Re: Javascript equivalent of #include or @import?

      In article <vfnr5rhk.fsf@h otpop.com>, Lasse Reichstein Nielsen <lrn@hotpop.com >
      writes:
      [color=blue]
      >After the page has loaded, it becomes harder. Some browsers allow you
      >to load a script by adding a new script tag to the page using DOM methods,
      >or by changing the src attribute of an existing tag, or by changing the
      >content of an existing script tag.
      >Not all browsers understand all, or any, of these methods, though,
      >so it's not something to depend on.[/color]

      Do you know of a browser that doesn't support dynamically loading a script
      file? Other than O7?

      <URL: http://tinyurl.com/d25q >

      I am still beating my head with Opera in general without resorting to an
      IFrames/Frames solution. But knowledge of any browser that won't handle the
      methods in the above URL would be of great use to me.

      --
      Randy

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Javascript equivalent of #include or @import?

        hikksnotathome@ aol.com (HikksNotAtHome ) writes:
        [color=blue]
        > Do you know of a browser that doesn't support dynamically loading a script
        > file? Other than O7?[/color]

        I'm running O7.5 preview, and it works there.
        I can do:

        var scr = document.create Element("script ");
        scr.type="text/javascript";
        document.body.a ppendChild(scr) ;
        scr.src = "../../alert.js"; // alerts something to show it has loaded.

        and also changing the text of a script element:

        document.getEle mentById("idOfS criptElement"). text = "alert('foo ')"

        You'll just have to wait for it to be released before it becomes
        widely used :)
        [color=blue]
        > <URL: http://tinyurl.com/d25q >[/color]

        (Please write what it is a link to. I usually don't follow links like
        this without a description).
        [color=blue]
        > I am still beating my head with Opera in general without resorting
        > to an IFrames/Frames solution. But knowledge of any browser that
        > won't handle the methods in the above URL would be of great use to
        > me.[/color]

        That article looks very thorough. I am not familiar with browsers
        outside of Netscape, Mozilla/Gecko, IE and Opera (but of those, I have
        17 versions installed :). I'll assume that Netscape 2 and 3 won't work
        either, nor earlier Operas, but I don't really care enough to test :)

        /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

        • Weston C

          #5
          Re: Javascript equivalent of #include or @import?

          Thought:



          So, to import a javascript file from inside of javascript:

          function include(url)
          {
          xmlhttp.open("G ET", url,true);
          xmlhttp.onready statechange=fun ction() {
          eval(xmlhttp.re sponseText);
          }
          xmlhttp.send()
          }

          I haven't tried this out yet; xmlhttp does not appear to work with
          Camino on Mac OS X (Gecko based) so I'm not sure what its coverage is,
          and I don't know exactly how it works.
          But this looks like a good lead to me...

          ~==~

          weston8[at]cann8central.or g
          (remove eights to email me)

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • HikksNotAtHome

            #6
            Re: Javascript equivalent of #include or @import?

            In article <3ff8a68d$0$202 $75868355@news. frii.net>, Weston C
            <west8on[at]cann8central.Re moveEights.org> writes:
            [color=blue]
            >Thought:
            >
            >http://jibbering.com/2002/4/httprequest.html
            >
            >So, to import a javascript file from inside of javascript:
            >
            >function include(url)
            >{
            > xmlhttp.open("G ET", url,true);
            > xmlhttp.onready statechange=fun ction() {
            > eval(xmlhttp.re sponseText);
            > }
            > xmlhttp.send()
            >}
            >
            >I haven't tried this out yet; xmlhttp does not appear to work with
            >Camino on Mac OS X (Gecko based) so I'm not sure what its coverage is,
            >and I don't know exactly how it works.
            >But this looks like a good lead to me...[/color]

            Please quote what you are replying to.

            Attempting to dynamically load a script file is going to be a lot more widely
            supported than an HTTPRequest object is.

            Since you have a Mac, can you test some of the methods in the article I linked
            to?
            <URL: http://tinyurl.com/d25q > is an article that lists several ways to try to
            dynamically load a .js file - on the fly.
            --
            Randy

            Comment

            • HikksNotAtHome

              #7
              Re: Javascript equivalent of #include or @import?

              In article <n0935ode.fsf@h otpop.com>, Lasse Reichstein Nielsen <lrn@hotpop.com >
              writes:
              [color=blue]
              >hikksnotathome @aol.com (HikksNotAtHome ) writes:
              >[color=green]
              >> Do you know of a browser that doesn't support dynamically loading a script
              >> file? Other than O7?[/color]
              >
              >I'm running O7.5 preview, and it works there.
              >I can do:
              >
              > var scr = document.create Element("script ");
              > scr.type="text/javascript";
              > document.body.a ppendChild(scr) ;
              > scr.src = "../../alert.js"; // alerts something to show it has loaded.
              >
              >and also changing the text of a script element:
              >
              > document.getEle mentById("idOfS criptElement"). text = "alert('foo ')"
              >
              >You'll just have to wait for it to be released before it becomes
              >widely used :)[/color]

              Nice to know that sometime in the next 6 months that it will work in a widely
              used version of Opera.

              [color=blue][color=green]
              >> <URL: http://tinyurl.com/d25q >[/color]
              >
              >(Please write what it is a link to. I usually don't follow links like
              >this without a description).[color=green]
              >> I am still beating my head with Opera in general without resorting
              >> to an IFrames/Frames solution. But knowledge of any browser that
              >> won't handle the methods in the above URL would be of great use to
              >> me.[/color]
              >
              >That article looks very thorough. I am not familiar with browsers
              >outside of Netscape, Mozilla/Gecko, IE and Opera (but of those, I have
              >17 versions installed :). I'll assume that Netscape 2 and 3 won't work
              >either, nor earlier Operas, but I don't really care enough to test :)[/color]

              I can save you the trouble of testing in NN2,3 or earlier Operas, it won't work
              there. Out of them, the only one I really care to try to make it work in is
              Opera.
              --
              Randy

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Javascript equivalent of #include or @import?

                Lasse Reichstein Nielsen wrote:
                [color=blue]
                > Weston C <west8on[at]cann8central.Re moveEights.org> writes:[color=green]
                >> I'm wondering if there's a way to include other pieces of javascript
                >> code from *within* javascript.... a la #include in C, @import in Java,
                >> use/require in Perl, include()/require() in PHP, etc....[/color]
                >
                > Not in general, no. If you want to include it while the page is being
                > loaded, you can use document.write to write a new Javascript tag:
                >
                > document.write( '<script type="text/javascript src="..."></script>');[/color]

                The missing delimiter for the "type" attribute aside, this will
                firstly definitely[tm] break in NN4 due to the NRLB[1], secondly
                break in for-this-point SGML-compliant parsers (finding the ETAGO
                delimiter "</" as end of the "script" element it needs to be
                enclosed in, thus passing an unterminated string to the script
                engine), and is thirdly not required at all if the inclusion is
                unconditional. Instead, a simple

                <script type="text/javascript" src="..."></script>

                suffices in the latter case and will do no harm to noscript users.
                If conditional inclusion is required, the best bet is

                <script type="text/javascript">
                <!--
                document.write( '<script type="text/javascript" src="..."><\/script>');
                //-->
                </script>

                but the NRLB issue is nevertheless (in)valid, so you better not count
                on it. Instead, one should either find a way to write that "script"
                element dynamically server-side or disregard all NN4 users (but the
                latter is not a wise decision for a commercial site).


                PointedEars
                ___________
                [1] Netscape Run-Length Bug: Non-reproducible bug making
                NN4 throwing away/overwriting parts of the source code.

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: Javascript equivalent of #include or @import?

                  Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                  [color=blue]
                  > Lasse Reichstein Nielsen wrote:[/color]
                  [color=blue][color=green]
                  >> Not in general, no. If you want to include it while the page is being
                  >> loaded, you can use document.write to write a new Javascript tag:
                  >>
                  >> document.write( '<script type="text/javascript src="..."></script>');[/color]
                  >
                  > The missing delimiter for the "type" attribute aside,[/color]

                  Bummer!
                  [color=blue]
                  > this will firstly definitely[tm] break in NN4 due to the NRLB[1],[/color]

                  That one I was not aware of. It's worth worrying about if one wants
                  to support Netscape 4.
                  [color=blue]
                  > secondly break in for-this-point SGML-compliant parsers (finding the
                  > ETAGO delimiter "</" as end of the "script" element it needs to be
                  > enclosed in, thus passing an unterminated string to the script
                  > engine),[/color]

                  That was deliberate. I did not write the surrounding script tags,
                  and in that case, I don't assume by default that the code is embedded
                  in HTML/XHTML. So, the code is plain Javascript and will work if
                  included in an external javascript file.
                  [color=blue]
                  > and is thirdly not required at all if the inclusion is
                  > unconditional.[/color]

                  To solve the original poster's problem, you can't depend on HTML.
                  The inclusions should be made by the Javascript (presumably so
                  you can change the inclusion without changing existing pages).

                  At that, it is probably not as good a solution as could be hoped for,
                  because the declarations of the included page will not be immediately
                  available to the file loading it, only to subsequent script elements,
                  or code running after the included file has been parsed.

                  [color=blue]
                  > suffices in the latter case and will do no harm to noscript users.
                  > If conditional inclusion is required, the best bet is
                  >
                  > <script type="text/javascript">
                  > <!--
                  > document.write( '<script type="text/javascript" src="..."><\/script>');
                  > //-->
                  > </script>[/color]

                  I wouldn't add <!-- and //-->. I prefer to write correct ECMAScript,
                  and there is no current browser that requires them.
                  [color=blue]
                  > but the NRLB issue is nevertheless (in)valid, so you better not count
                  > on it.[/color]

                  I'll look into it.
                  /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

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Javascript equivalent of #include or @import?

                    Lasse Reichstein Nielsen wrote:
                    [color=blue]
                    > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                    >> Lasse Reichstein Nielsen wrote:[color=darkred]
                    >>> Not in general, no. If you want to include it while the page is being
                    >>> loaded, you can use document.write to write a new Javascript tag:
                    >>>
                    >>> document.write( '<script type="text/javascript src="..."></script>');[/color]
                    >>
                    >> [...]
                    >> secondly break in for-this-point SGML-compliant parsers (finding the
                    >> ETAGO delimiter "</" as end of the "script" element it needs to be
                    >> enclosed in, thus passing an unterminated string to the script
                    >> engine),[/color]
                    >
                    > That was deliberate. I did not write the surrounding script tags,
                    > and in that case, I don't assume by default that the code is embedded
                    > in HTML/XHTML.[/color]

                    ACK, though it is better to mention the ETAGO issue to prevent
                    the unexperienced from running into trouble on this.
                    [color=blue][color=green]
                    >> and is thirdly not required at all if the inclusion is
                    >> unconditional.[/color]
                    >
                    > To solve the original poster's problem, you can't depend on HTML.[/color]

                    | I'm wondering if there's a way to include other pieces of javascript
                    | code from *within* javascript.... a la #include in C, @import in Java,
                    | use/require in Perl, include()/require() in PHP, etc....

                    You can.
                    [color=blue]
                    > The inclusions should be made by the Javascript (presumably so
                    > you can change the inclusion without changing existing pages).[/color]

                    That would not be only of academical value, while causing problems with
                    dynamically written "script" elements in practice. (No, I do not mean
                    only the NRLB.)
                    [color=blue]
                    > At that, it is probably not as good a solution as could be hoped for,
                    > because the declarations of the included page[/color]

                    Page?
                    [color=blue]
                    > will not be immediately available to the file loading it, only to
                    > subsequent script elements, or code running after the included file
                    > has been parsed.[/color]

                    Which is exactly the nature of the pendants in other languages mentioned
                    above.
                    [color=blue][color=green]
                    >> suffices in the latter case and will do no harm to noscript users.
                    >> If conditional inclusion is required, the best bet is
                    >>
                    >> <script type="text/javascript">
                    >> <!--
                    >> document.write( '<script type="text/javascript" src="..."><\/script>');
                    >> //-->
                    >> </script>[/color]
                    >
                    > I wouldn't add <!-- and //-->. I prefer to write correct ECMAScript,
                    > and there is no current browser that requires them.[/color]

                    Depends on what you call "current browser". Besides,
                    there is no "current browser" that supports ECMAScript.


                    PointedEars

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: Javascript equivalent of #include or @import?

                      Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                      [color=blue]
                      > Lasse Reichstein Nielsen wrote:
                      >[color=green]
                      >> Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=darkred]
                      >>> Lasse Reichstein Nielsen wrote:
                      >>>> Not in general, no. If you want to include it while the page is being
                      >>>> loaded, you can use document.write to write a new Javascript tag:
                      >>>>
                      >>>> document.write( '<script type="text/javascript src="..."></script>');
                      >>>
                      >>> [...]
                      >>> secondly break in for-this-point SGML-compliant parsers (finding the
                      >>> ETAGO delimiter "</" as end of the "script" element it needs to be
                      >>> enclosed in, thus passing an unterminated string to the script
                      >>> engine),[/color]
                      >>
                      >> That was deliberate. I did not write the surrounding script tags,
                      >> and in that case, I don't assume by default that the code is embedded
                      >> in HTML/XHTML.[/color]
                      >
                      > ACK, though it is better to mention the ETAGO issue to prevent
                      > the unexperienced from running into trouble on this.[/color]

                      It is reasonable. I shall consider wrapping in script tags when
                      appropriate.

                      [color=blue]
                      > | I'm wondering if there's a way to include other pieces of javascript
                      > | code from *within* javascript.... a la #include in C, @import in Java,
                      > | use/require in Perl, include()/require() in PHP, etc....
                      >
                      > You can.[/color]

                      Is there a method that works consistently across modern browsers?
                      (rules out XMLHTTP, but probably not iframes).
                      [color=blue]
                      > That would not be only of academical value, while causing problems with
                      > dynamically written "script" elements in practice. (No, I do not mean
                      > only the NRLB.)[/color]

                      What other problems are there with dynamically written script elements?
                      [color=blue]
                      > Page?[/color]

                      Script. Doh.
                      [color=blue][color=green]
                      >> will not be immediately available to the file loading it, only to
                      >> subsequent script elements, or code running after the included file
                      >> has been parsed.[/color]
                      >
                      > Which is exactly the nature of the pendants in other languages mentioned
                      > above.[/color]

                      No, when they have an include statement at the top of a script, the
                      included material is available to the remainder of that script. Using
                      document.write of a script tag will only make the included material
                      available to subsequent script tags, not the remainder of the
                      execution of the one contining the document.write.
                      [color=blue]
                      > Depends on what you call "current browser". Besides,[/color]

                      Anything more recent than Netscape 3?
                      [color=blue]
                      > there is no "current browser" that supports ECMAScript.[/color]

                      Probably correct. The ones I have tested all fail on this script:
                      <script type="text/javascript">
                      var z = 1;
                      if ( 0 <!-- z
                      ) { alert(42); }
                      </script>
                      It should execute alert(42) according to the ECMAScript standard.

                      /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

                      • Dr John Stockton

                        #12
                        Re: Javascript equivalent of #include or @import?

                        JRS: In article <eku0pzer.fsf@h otpop.com>, seen in
                        news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
                        posted at Fri, 16 Jan 2004 00:25:48 :-[color=blue]
                        >
                        >Probably correct. The ones I have tested all fail on this script:
                        > <script type="text/javascript">
                        > var z = 1;
                        > if ( 0 <!-- z
                        > ) { alert(42); }
                        > </script>
                        >It should execute alert(42) according to the ECMAScript standard.
                        >[/color]

                        That code works in <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>,
                        which uses eval in a fully-loaded page, in my MSIE4.

                        It also works in a being-loaded page if converted to a string parameter
                        to eval.

                        It does nothing in a being-loaded page if inserted as published.

                        AISB, eval has its uses <g>.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                        Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
                        Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

                        Comment

                        • gendem

                          #13
                          Re: Javascript equivalent of #include or @import?

                          >>> I'm wondering if there's a way to include other pieces of javascript[color=blue][color=green][color=darkred]
                          >>> code from *within* javascript.... a la #include in C, @import in Java,
                          >>> use/require in Perl, include()/require() in PHP, etc....[/color]
                          >>
                          >> Not in general, no. If you want to include it while the page is being
                          >> loaded, you can use document.write to write a new Javascript tag:[/color][/color]

                          Rats. I'm trying to script in PhotoShop, which uses Javascript, and
                          it sure is a waste to have to copy code around instead of just
                          including it. I guess it's a limitation of not seeing all potential
                          uses of a language before hand. Oh well.

                          Comment

                          • seven

                            #14
                            Re: Javascript equivalent of #include or @import?

                            On Fri, 16 Jan 2004 20:43:32 -0500, gendem <gendem@yahoo.c om> wrote:
                            [color=blue]
                            >Rats. I'm trying to script in PhotoShop, which uses Javascript, and
                            >it sure is a waste to have to copy code around instead of just
                            >including it. I guess it's a limitation of not seeing all potential
                            >uses of a language before hand. Oh well.[/color]

                            PhotoShop? Did you mean DreamWeaver?



                            ---

                            seven at post modern dot com
                            If sending an email, +add+ the word
                            "newsgroup" to the subject line to
                            temporarily avert the spam filter.

                            Comment

                            • Simon Wigzell

                              #15
                              Re: Javascript equivalent of #include or @import?

                              To load any file into the current page from the server use :
                              <!--#include file="[filename]"-->This is server side code.Javascript runs on
                              the client so unless everyone who is going to visit your website has the
                              java files on their computers, what you want would not work!"gendem"
                              <gendem@yahoo.c om> wrote in message
                              news:in4h00he9i lt5navjh6hvcdud 0lt6jea7u@4ax.c om...[color=blue][color=green][color=darkred]
                              > >>> I'm wondering if there's a way to include other pieces of javascript
                              > >>> code from *within* javascript.... a la #include in C, @import in Java,
                              > >>> use/require in Perl, include()/require() in PHP, etc....
                              > >>
                              > >> Not in general, no. If you want to include it while the page is being
                              > >> loaded, you can use document.write to write a new Javascript tag:[/color][/color]
                              >
                              > Rats. I'm trying to script in PhotoShop, which uses Javascript, and
                              > it sure is a waste to have to copy code around instead of just
                              > including it. I guess it's a limitation of not seeing all potential
                              > uses of a language before hand. Oh well.[/color]


                              Comment

                              Working...