import/include a .js file into a .js file

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

    #1

    import/include a .js file into a .js file

    Hi,

    Is there a way to include a .js file inside a .js file in JavaScript 1.5?

    Thanx

    Henri



  • kaeli

    #2
    Re: import/include a .js file into a .js file

    In article <419e1eb9$0$910 5$8fcfb975@news .wanadoo.fr>, hmfireball@hotm ail.com
    enlightened us with...[color=blue]
    > Hi,
    >
    > Is there a way to include a .js file inside a .js file in JavaScript 1.5?
    >[/color]

    Not that I know of. If you figure one out, I'd love to see it. I'm tired of
    having to copy/paste library functions, too.

    --
    --
    ~kaeli~
    If a turtle doesn't have a shell, is he homeless or naked?



    Comment

    • Randy Webb

      #3
      Re: import/include a .js file into a .js file

      kaeli wrote:
      [color=blue]
      > In article <419e1eb9$0$910 5$8fcfb975@news .wanadoo.fr>, hmfireball@hotm ail.com
      > enlightened us with...
      >[color=green]
      >>Hi,
      >>
      >>Is there a way to include a .js file inside a .js file in JavaScript 1.5?
      >>[/color]
      >
      >
      > Not that I know of. If you figure one out, I'd love to see it. I'm tired of
      > having to copy/paste library functions, too.
      >[/color]

      In the included file either document.write the script tag to include the
      second file, or, createElement and load it.

      Of course, with server side coding, it becomes trivial to add an include
      statement to include the external file.

      --
      Randy
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • kaeli

        #4
        Re: import/include a .js file into a .js file

        In article <GNqdnbmOd6DjrA PcRVn-gA@comcast.com> , HikksNotAtHome@ aol.com
        enlightened us with...[color=blue]
        >
        > In the included file either document.write the script tag to include the
        > second file, or, createElement and load it.
        >
        > Of course, with server side coding, it becomes trivial to add an include
        > statement to include the external file.
        >[/color]

        I don't think that would work for what I want it for.

        I have a library file called jsValidation.js with generic functions for
        things like testing for blank fields, format of phone number, and so on.

        I also have one called jsEvents.js with a way to add events in a cross-
        browser fashion. It has a function called addEvent.

        Now, say I want to make a third js file and I want to call functions isBlank
        from the validation file and addEvent from the event file.

        I don't think I can do that. Do you know a way?

        --
        --
        ~kaeli~
        If God dropped acid, would he see people?



        Comment

        • Rob B

          #5
          Re: import/include a .js file into a .js file

          Unless I'm misunderstandin g this, you're barking up the wrong tree here.
          You don't call js functions from files, you load the files into RAM (the
          js memory space) and then simply simply call any functions needed; if in
          scope, they'll run. The only difference between embedded (hardcoded) js
          and 'external' js is that one is - erm - embedded in the main document
          and read into memory from there, and the other is stored in a separate
          file and loaded from there. No other distinctions: 'external' js doesn't
          sort of "hover out there" as many seem to thing, waiting to be "called".
          It's either loaded or it's not, available or not.

          There are apis with member functions specifically designed to 'import'
          needed external files (using document.write to print script tags) but
          they're highly proprietary.



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

          Comment

          • Rob B

            #6
            Re: import/include a .js file into a .js file

            kaeli...

            btw love your sig..Totally 60s. :D

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

            Comment

            • kaeli

              #7
              Re: import/include a .js file into a .js file

              In article <419e48bd$0$243 35$c397aba@news .newsgroups.ws> , ferndoc9
              @hotmail.com enlightened us with...[color=blue]
              > Unless I'm misunderstandin g this, you're barking up the wrong tree here.[/color]

              Not quite. But I did make a bit of a logic error. I'm too used to compiled
              code. ;)
              I want to ensure both files are loaded into memory when only one is
              specifically included in the client html file with <script src=> type tag.

              Since I only use script in my intranet apps, and we only support DOM
              browsers, I see now that I can simply add the other files like so:

              /* jsHandler.js */
              function inc(filename)
              {
              var body = document.getEle mentsByTagName( 'body').item(0) ;
              script = document.create Element('script ');
              script.src = filename;
              script.type = 'text/javascript';
              body.appendChil d(script)
              }

              inc("jsValidati on.js");
              inc("jsEvents.j s");

              /* several more functions that call functions in the above two files */
              function blah (foo)
              {
              if (isBlank(foo)) return false;
              }

              /* end */

              Then in my JSP, I can just do
              <script type="text/javascript" src="jsHandler. js"></script>
              and it will get both the other files as well.

              I suspect there would be problems with scripts that try to run prior to page
              load (such as image preloaders), but none of mine do that. My stuff is pretty
              much all form validation and dynamically adding event handlers to JSP-
              generated form elements.

              Thanks for the tips.

              --
              --
              ~kaeli~
              Profanity: the single language in which all programmers are
              expert.



              Comment

              • Henri

                #8
                Re: import/include a .js file into a .js file

                This is the best solution I've found so far:

                At the beginning of each .js file importing another file:
                (this file defines a class called 'myclass' and 'km_' is just the prefix I
                use for my functions or classes)

                ----------------------------------------------------------------
                // myclass.js

                if (typeof(km_scri pts) == 'undefined') var km_scripts = new Object();
                km_myclass_impo rt('importedfil e.js');

                function km_myclass_impo rt(jsFile) {
                if (km_scripts[jsFile] != null) return;
                var scriptElt = document.create Element('script ');
                scriptElt.type = 'text/javascript';
                scriptElt.src = jsFile;
                document.getEle mentsByTagName( 'head')[0].appendChild(sc riptElt);
                km_scripts[jsFile] = jsFile; // or whatever value your prefer
                }

                function km_myclass_aler t() {
                alert(importedV alue);
                }

                -------------------------------------------------------------
                The imported file importedfile.js :

                // importedfile.js

                var importedValue = 'The file was imported';

                ----------------------------------------------------------------
                The HTML file to test it :

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                <HTML>
                <HEAD>
                <script language="javas cript" src="myclass.js " />
                <TITLE></TITLE>
                </HEAD>
                <BODY>
                <input type="button" value="Test!" onclick="km_myc lass_alert()" />
                </BODY>
                </HTML>


                Thanx for your helps
                Henri


                "kaeli" <tiny_one@NOSPA M.comcast.net> a écrit dans le message de
                news:MPG.1c07de 282427786398a2b 0@nntp.lucent.c om...[color=blue]
                > In article <419e1eb9$0$910 5$8fcfb975@news .wanadoo.fr>,[/color]
                hmfireball@hotm ail.com[color=blue]
                > enlightened us with...[color=green]
                > > Hi,
                > >
                > > Is there a way to include a .js file inside a .js file in JavaScript[/color][/color]
                1.5?[color=blue][color=green]
                > >[/color]
                >
                > Not that I know of. If you figure one out, I'd love to see it. I'm tired[/color]
                of[color=blue]
                > having to copy/paste library functions, too.
                >
                > --
                > --
                > ~kaeli~
                > If a turtle doesn't have a shell, is he homeless or naked?
                > http://www.ipwebdesign.net/wildAtHeart
                > http://www.ipwebdesign.net/kaelisSpace
                >
                >[/color]



                Comment

                • Randy Webb

                  #9
                  Re: import/include a .js file into a .js file

                  kaeli wrote:
                  [color=blue]
                  > In article <GNqdnbmOd6DjrA PcRVn-gA@comcast.com> , HikksNotAtHome@ aol.com
                  > enlightened us with...
                  >[color=green]
                  >>In the included file either document.write the script tag to include the
                  >>second file, or, createElement and load it.
                  >>
                  >>Of course, with server side coding, it becomes trivial to add an include
                  >>statement to include the external file.
                  >>[/color]
                  >
                  >
                  > I don't think that would work for what I want it for.
                  >
                  > I have a library file called jsValidation.js with generic functions for
                  > things like testing for blank fields, format of phone number, and so on.
                  >
                  > I also have one called jsEvents.js with a way to add events in a cross-
                  > browser fashion. It has a function called addEvent.
                  >
                  > Now, say I want to make a third js file and I want to call functions isBlank
                  > from the validation file and addEvent from the event file.
                  >
                  > I don't think I can do that. Do you know a way?
                  >[/color]

                  <script src="myCustomJS File.php" type="text/javascript"></script>

                  And in myCustomJSFile. php:

                  include 'jsValidation.j s';
                  include 'jsEvents.js';
                  include 'thirdJSFile.js ';

                  //rest of unique to the page JS code here


                  When the browser gets it, all in one file, are all the functions/code
                  you want.

                  --
                  Randy
                  comp.lang.javas cript FAQ - http://jibbering.com/faq

                  Comment

                  Working...