Including javascript within a js file

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

    Including javascript within a js file

    I'm looking for a way to include javascript files from within a ".js"
    file. This would allow me to only need to link to one ".js" file, and
    yet still organize my functions into non gargantuan files for easy
    editing. I'm hoping there is some sort of include or import directive
    that I could use. Or if no such directive exists, I'm wondering if
    anyone has written one which I could use.

    I need to do this without any server side scripting. For now at least,
    the html is being used locally with local files.

    I've found this:
    http://www.forum4designers.com/archi...4-1-28286.html post about
    the same problem, and was hoping that either someone here would have
    more light to shed on the issue, or that there might be a way to do
    this in the special situation of just working on a local, writable
    drive.

    -Trenqo

  • ASM

    #2
    Re: Including javascript within a js file

    Trenqo 0 wrote:[color=blue]
    > I'm looking for a way to include javascript files from within a ".js"
    > file. This would allow me to only need to link to one ".js" file, and
    > yet still organize my functions into non gargantuan files for easy
    > editing.[/color]

    <script type="text/javascript">
    // <![CDATA[

    function includejs(file) {
    document.write( '<script src="' + file +
    '"type="text/javascript"><\/script>');
    }

    function one() { blah }
    function two() { bloh }
    includejs('foo. js');
    function three() { bluh }

    // ]]>
    </script>


    --
    Stephane Moriaux et son [moins] vieux Mac

    Comment

    • Richard Cornford

      #3
      Re: Including javascript within a js file

      ASM wrote:
      <snip>[color=blue]
      > <script type="text/javascript">
      > // <![CDATA[[/color]

      In an HTML document the content type of a script element is CDATA. The
      javascript comment makes this a redundant practice in an HTML document,
      doubly redundant because the content is CDATA to start with.
      [color=blue]
      > function includejs(file) {
      > document.write( '<script src="' + file +
      > '"type="text/javascript"><\/script>');
      > }[/color]

      To date XHTML DOMs have tended not to support the - document.write -
      method. Finding a call to the - document.write - method inside a
      <![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
      being used and its relationship to the DOM being scripted.

      Richard.


      Comment

      • RobG

        #4
        Re: Including javascript within a js file

        Trenqo 0 wrote:[color=blue]
        > I'm looking for a way to include javascript files from within a ".js"
        > file. This would allow me to only need to link to one ".js" file, and
        > yet still organize my functions into non gargantuan files for easy
        > editing. I'm hoping there is some sort of include or import directive
        > that I could use. Or if no such directive exists, I'm wondering if
        > anyone has written one which I could use.[/color]

        Use the following:

        Include the following tag the HTML file(s) just after the body tag:

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


        The file 'include.js' contains:

        var scripts = [
        'script_1.js',
        'script_2.js'
        ];
        if ( document.getEle mentsByTagName ) {
        var head = document.getEle mentsByTagName( 'HEAD')[0]
        var scriptElement;
        var i = scripts.length;
        while ( i-- ) {
        scriptElement = document.create Element('script ');
        scriptElement.t ype = 'text/javascript';
        scriptElement.s rc = scripts[i];
        head.appendChil d(scriptElement );
        }
        }


        I seem to be able to get away with putting the script above script
        element in the head, but I'm not sure that will be OK for all browsers.
        It should be safe as the first element of the body.
        [color=blue]
        >
        > I need to do this without any server side scripting. For now at least,
        > the html is being used locally with local files.[/color]

        Provided the links point to the files, life will be fine. You may need
        to check the order in which files are loaded, you don't want to be
        calling functions that aren't loaded yet.

        [...]

        --
        Rob

        Comment

        • VK

          #5
          Re: Including javascript within a js file

          > I'm looking for a way to include javascript files[color=blue]
          > from within a ".js" file.[/color]

          Well, the trully orthodox way is by using "import" instruction inside
          of your code.

          The catch 22 is that both main script and the "source" script have to
          be signed by the same principals (So say if the main script signed by
          John Doe, you can import only from archives signed by John Doe).

          The catch 23 is that FF needs signed .jar file with JavaScript in it.
          And IE needs signed .cab with JScript

          All of above makes the import technologies nearly impossible. Over the
          7 years this technologies exists I saw just a few such solutions, and
          all of them for inside use (where the compatibility is not such an
          issue).

          So I would stay with RobG. Still good to know other options, I guess :-)

          Comment

          • ASM

            #6
            Re: Including javascript within a js file

            Richard Cornford wrote:[color=blue]
            > ASM wrote:
            > <snip>
            >[color=green]
            >><script type="text/javascript">
            >>// <![CDATA[[/color]
            >
            >
            > In an HTML document the content type of a script element is CDATA. The
            > javascript comment makes this a redundant practice in an HTML document,
            > doubly redundant because the content is CDATA to start with.[/color]

            My english being quite poor ... do you mean
            <![CDATA[ ... ]]>
            is not to use ?
            (it is what I understand)

            If that's right, why Tidy (from W3C) use that ?
            [color=blue][color=green]
            >>function includejs(file) {
            >>document.writ e('<script src="' + file +
            >> '"type="text/javascript"><\/script>');
            >>}[/color]
            >
            >
            > To date XHTML DOMs have tended not to support the - document.write -
            > method.[/color]

            Most of time I do javascript for NC4.5
            Sometimes I add few DOM code when this code is only cosmetic
            and of no importance in general fonctionalities of the html page.
            [color=blue]
            > Finding a call to the - document.write - method inside a
            > <![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
            > being used and its relationship to the DOM being scripted.[/color]

            So, how have I to write correctly (with a transitional doctype)
            <script type= ....
            and
            this document.write( ) ?
            [color=blue]
            > Richard.[/color]



            --
            Stephane Moriaux et son [moins] vieux Mac

            Comment

            • ASM

              #7
              Re: Including javascript within a js file

              RobG wrote:[color=blue]
              >
              > Use the following:
              >
              > Include the following tag the HTML file(s) just after the body tag:
              >
              > <script type="text/javascript" src="include.js "></script>
              >
              >
              > The file 'include.js' contains:
              >
              > var scripts = [
              > 'script_1.js',
              > 'script_2.js'
              > ];
              > if ( document.getEle mentsByTagName ) {[/color]

              Oh yes ! instersting method ! and for my NC 4.5 ?

              [color=blue]
              > var head = document.getEle mentsByTagName( 'HEAD')[0]
              > var scriptElement;
              > var i = scripts.length;
              > while ( i-- ) {
              > scriptElement = document.create Element('script ');
              > scriptElement.t ype = 'text/javascript';
              > scriptElement.s rc = scripts[i];
              > head.appendChil d(scriptElement );
              > }
              > }[/color]



              --
              Stephane Moriaux et son [moins] vieux Mac

              Comment

              • RobG

                #8
                Re: Including javascript within a js file

                ASM wrote:
                [...][color=blue]
                >
                > Oh yes ! instersting method ! and for my NC 4.5 ?
                >[/color]

                The scripts will not be added as Netscape Navigator 4 (NN4) does not
                support getElementsByTa gName.

                If all JS functionality is added using the suggested method, NN4 users
                will get the default site without JavaScript - which may be a distinct
                advantage, given that the use of NN4 indicates a very old PC that may
                perform better without unnecessary scripts slowing it down.


                --
                Rob

                Comment

                • ASM

                  #9
                  Re: Including javascript within a js file

                  RobG wrote:[color=blue]
                  > ASM wrote:
                  > [...]
                  >[color=green]
                  >>
                  >> Oh yes ! instersting method ! and for my NC 4.5 ?
                  >>[/color]
                  >
                  > The scripts will not be added as Netscape Navigator 4 (NN4) does not
                  > support getElementsByTa gName.[/color]

                  it was the why of my question
                  [color=blue]
                  > If all JS functionality is added using the suggested method, NN4 users
                  > will get the default site without JavaScript - which may be a distinct
                  > advantage, given that the use of NN4 indicates a very old PC that may
                  > perform better without unnecessary scripts slowing it down.[/color]

                  we'll say that :-/

                  I keep your script as a good example of objects creation and placement

                  --
                  Stephane Moriaux et son [moins] vieux Mac

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Including javascript within a js file

                    ASM schrieb:
                    [color=blue]
                    > Richard Cornford wrote:[color=green]
                    > > ASM wrote:[color=darkred]
                    > >> <script type="text/javascript">
                    > >> // <![CDATA[[/color]
                    > >
                    > > In an HTML document the content type of a script element is CDATA.
                    > > The javascript comment makes this a redundant practice in an HTML
                    > > document, doubly redundant because the content is CDATA to start
                    > > with.[/color]
                    >
                    > My english being quite poor ... do you mean
                    > <![CDATA[ ... ]]>
                    > is not to use ?
                    > (it is what I understand)[/color]

                    It is not to be used in HTML documents served as text/html and JS
                    script files, and it is not necessary to be commented out in XHTML
                    documents served as application/xhtml+xml; serving XHTML as
                    text/html is considered harmful.
                    [color=blue]
                    > If that's right, why Tidy (from W3C) use that ?[/color]

                    Because it is a little bit buggy. Note: HTML Tidy is not from W3C,
                    it was originally developed by a W3C member and is now maintained by
                    several developers, obtainable from SourceForge.
                    [color=blue][color=green][color=darkred]
                    > >>function includejs(file) {
                    > >>document.writ e('<script src="' + file +
                    > >> '"type="text/javascript"><\/script>');
                    > >>}[/color]
                    > >
                    > >
                    > > To date XHTML DOMs have tended not to support the - document.write
                    > > - method.[/color]
                    >
                    > Most of time I do javascript for NC4.5
                    > Sometimes I add few DOM code when this code is only cosmetic
                    > and of no importance in general fonctionalities of the html page.[/color]

                    But in this case it is, you just didn't recognize it.
                    [color=blue][color=green]
                    > > Finding a call to the - document.write - method inside a
                    > > <![CDATA[ ... ]]> wrapper suggests misconceptions about the mark-up
                    > > being used and its relationship to the DOM being scripted.[/color]
                    >
                    > So, how have I to write correctly (with a transitional doctype)
                    > <script type= ....
                    > and
                    > this document.write( ) ?[/color]

                    Using node-accessing and node-creating methods introduced in W3C DOM
                    Level 1 HTML and DOM Level 2 Core; using the proprietary variants if
                    you need to.


                    PointedEars

                    Comment

                    Working...