How to outsource Javascript code from html code ?

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

    How to outsource Javascript code from html code ?

    Is there a way of extracting the Javascript code from the "normal" HTML code
    (e.g. similar to CSS code which can be put into a separate file) ?

    If you offer a solution: can I determine in your solution where and which part
    of the code should be placed/inserted in the main HTML code?

    Sometimes a Javascript functionality is split up to several parts and/or
    it makes a big difference where the Javascript code should run/be called
    (e.g.
    - in the <HEAD>....</HEAD> or
    - at the beginning just behind the <BODY> tag
    - at the end just before the </BODY> tag.
    .....)

    Arty

  • Hywel Jenkins

    #2
    Re: How to outsource Javascript code from html code ?

    In article <bssh5d$l7c$03$ 1@news.t-online.com>, artyC@redseven. com
    says...[color=blue]
    > Is there a way of extracting the Javascript code from the "normal" HTML code
    > (e.g. similar to CSS code which can be put into a separate file) ?
    >
    > If you offer a solution: can I determine in your solution where and which part
    > of the code should be placed/inserted in the main HTML code?
    >
    > Sometimes a Javascript functionality is split up to several parts and/or
    > it makes a big difference where the Javascript code should run/be called
    > (e.g.
    > - in the <HEAD>....</HEAD> or
    > - at the beginning just behind the <BODY> tag
    > - at the end just before the </BODY> tag.[/color]

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

    --
    Hywel I do not eat quiche


    Comment

    • DU

      #3
      Re: How to outsource Javascript code from html code ?

      Arthur Connor wrote:[color=blue]
      > Is there a way of extracting the Javascript code from the "normal" HTML code
      > (e.g. similar to CSS code which can be put into a separate file) ?
      >[/color]

      I'm not sure what you mean with "extracting ".

      There is a bookmarklet available which will allow you to view all
      javascript code in a given HTML page.

      You can put all script functions and code into a single external
      javascript file.
      [color=blue]
      > If you offer a solution: can I determine in your solution where and which part
      > of the code should be placed/inserted in the main HTML code?
      >[/color]

      Again, your question is a bit fuzzy.

      If you use a customized bookmarklet, then yes you can figure out inline
      scripts and scripts coming from an external file.

      One can figure out where in a given HTML document the functions are called.

      [color=blue]
      > Sometimes a Javascript functionality is split up to several parts and/or
      > it makes a big difference where the Javascript code should run/be called
      > (e.g.
      > - in the <HEAD>....</HEAD> or
      > - at the beginning just behind the <BODY> tag
      > - at the end just before the </BODY> tag.
      > ....)
      >
      > Arty
      >[/color]

      That's because of some desired effects or because some functions should
      be executed before some others (like for rollover images) or because
      some functions should be executed while the page is loading (e.g.:
      document.write( ) calls) or because some other functions should be
      executed only once the page has finally and completely loaded (with the
      defer attribute or with the onload attribute of the body element or
      window object), etc... It all depends on the whole code, the targeted
      effects/results, etc.. There is no single and unique rule here.
      Overall, it is best to modularize javascript code into defined functions
      and it is best to stay away from document.write( ) calls.
      In general, I think it is best to create a single unique external
      javascript file so that
      - your code can be reused
      - your code can be cached and reused in the same session, reducing http
      downloads and parsing again, making script functions a bit more
      responsive, snappy.

      DU

      Comment

      • John McGaw

        #4
        Re: How to outsource Javascript code from html code ?

        "Arthur Connor" <artyC@redseven .com> wrote in message
        news:bssh5d$l7c $03$1@news.t-online.com...[color=blue]
        > Is there a way of extracting the Javascript code from the "normal" HTML[/color]
        code[color=blue]
        > (e.g. similar to CSS code which can be put into a separate file) ?
        >
        > If you offer a solution: can I determine in your solution where and which[/color]
        part[color=blue]
        > of the code should be placed/inserted in the main HTML code?
        >
        > Sometimes a Javascript functionality is split up to several parts and/or
        > it makes a big difference where the Javascript code should run/be called
        > (e.g.
        > - in the <HEAD>....</HEAD> or
        > - at the beginning just behind the <BODY> tag
        > - at the end just before the </BODY> tag.
        > ....)
        >
        > Arty
        >[/color]
        I guess it depends on what your exact usage is. At my site I have two
        different applications for js. One handles image swapping and popup display
        and depends on having functions defined in advance of them being used so I
        have the definitions included right before the end of the <head> as shown in
        this scrap:
        ....
        <title>
        Bodger&#39;s New Day
        </title>
        <!-- insert image routines -->
        <script type="text/javascript" src="theimager. js">
        </script>
        <!-- end of insert -->
        </head>
        ....

        And the actual call is made when a reader clicks on an image of a button and
        looks like this:

        ....
        <a href=
        "JavaScript : newWindow = openWin( &#39;gallery/gallery-01-small.jpg&#39;,
        600, 424 ); newWindow.focus ()">
        <img src="button/small.gif" class="gbutt" alt="" title="" width="30"
        height="20" /></a>
        ....

        where openWin() is defined earlier and is called with the required
        parameters here to create a new window with an image in it and the focus()
        pops the new window up over the parent.

        In the second main application there is no function definition so nothing
        needs to appear in the <head> so the code is placed where it is needed
        inline like this:

        ....
        <!-- Start of TheCounter.com Code -->
        <script type="text/javascript" src="thecounter .js">
        </script>
        <!-- End of TheCounter.com Code -->
        ....

        This is done because the js provided by thecounter.com is pretty ugly and
        totally blows away any attempts at validation.

        Is this what you were looking for?
        --
        John McGaw
        [Knoxville, TN, USA]

        Return address will not work. Please
        reply in group or through my website:


        Comment

        • Martin Fuchs

          #5
          Re: How to outsource Javascript code from html code ?

          "Arthur Connor" <artyC@redseven .com> wrote in message
          news:bssh5d$l7c $03$1@news.t-online.com...[color=blue]
          > Is there a way of extracting the Javascript code from the "normal" HTML[/color]
          code[color=blue]
          > (e.g. similar to CSS code which can be put into a separate file) ?[/color]

          outsourced java script looks like the following:

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

          just put your java script code (between the script tags) into a .txt file
          and rename it to a .js file
          [color=blue]
          > Sometimes a Javascript functionality is split up to several parts and/or
          > it makes a big difference where the Javascript code should run/be called
          > (e.g.
          > - in the <HEAD>....</HEAD> or
          > - at the beginning just behind the <BODY> tag
          > - at the end just before the </BODY> tag.[/color]

          as far as i know (i am not very good at java script) that depends of the
          type of java script you are using...

          hope i could help.

          martin


          Comment

          • Chris Leonard

            #6
            Re: How to outsource Javascript code from html code ?

            Where ever you use the <script> tag use it like this:

            <script src="myjavascri ptfile.js">
            </script>

            I think this is the right syntax, I know I'm on the right line!

            Chris


            Comment

            • Eric Bohlman

              #7
              Re: How to outsource Javascript code from html code ?

              artyC@redseven. com (Arthur Connor) wrote in
              news:bssh5d$l7c $03$1@news.t-online.com:
              [color=blue]
              > Is there a way of extracting the Javascript code from the "normal"
              > HTML code (e.g. similar to CSS code which can be put into a separate
              > file) ?[/color]

              Yup. Anywhere you can write

              <script type="text/javascript">som e script</script>

              you can also write

              <script src="urlforscri pt" type="text/javascript"></script>

              In addition to making your code more maintainable, linking to external
              scripts also speeds things up because browsers will cache the scripts
              rather than reloading them for each page.
              [color=blue]
              > If you offer a solution: can I determine in your solution where and
              > which part of the code should be placed/inserted in the main HTML
              > code?[/color]

              Same as if you were inlining the scripts.
              [color=blue]
              > Sometimes a Javascript functionality is split up to several parts
              > and/or it makes a big difference where the Javascript code should
              > run/be called (e.g.
              > - in the <HEAD>....</HEAD> or
              > - at the beginning just behind the <BODY> tag
              > - at the end just before the </BODY> tag.[/color]

              You just have to give each section of script its own URL.

              Comment

              • Toby A Inkster

                #8
                Re: How to outsource Javascript code from html code ?

                Arthur Connor wrote:
                [color=blue]
                > Is there a way of extracting the Javascript code from the "normal" HTML code
                > (e.g. similar to CSS code which can be put into a separate file) ?[/color]

                Of course.

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

                --
                Toby A Inkster BSc (Hons) ARCS
                Contact Me - http://www.goddamn.co.uk/tobyink/?page=132

                Comment

                • nice.guy.nige

                  #9
                  Re: How to outsource Javascript code from html code ?

                  While the city slept, Arthur Connor <artyC@redseven .com> feverishly typed:
                  [color=blue]
                  > Is there a way of extracting the Javascript code from the "normal"
                  > HTML code (e.g. similar to CSS code which can be put into a separate
                  > file) ?[/color]

                  Take the existing javascript code from your page (minus the opening <script
                  ....> and closing </script>) and save that as a file with extension .js. eg,
                  if you have the following...

                  <script type="text/javascript">
                  <!-- Hide

                  function myFunction()
                  {
                  // code here
                  }

                  // Dunhidin -->
                  </script>

                  .... cut everything from <!--Hide to // Dunhidin --> and save that as, for
                  example, "myJavascript.j s". Then add the following to your script tag...

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

                  Hope that help,
                  Nige

                  --
                  Nigel Moss.

                  Email address is not valid. nigel@nigenetDO G.org.uk. Take the dog out!
                  http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
                  In the land of the blind, the one-eyed man is very, very busy!


                  Comment

                  • Dr John Stockton

                    #10
                    How to outsource Javascript code from html code ?

                    JRS: In article <bssh5d$l7c$03$ 1@news.t-online.com>, seen in
                    news:comp.lang. javascript, Arthur Connor <artyC@redseven .com> posted at
                    Tue, 30 Dec 2003 19:46:07 :-[color=blue]
                    >Is there a way of extracting the Javascript code from the "normal" HTML code
                    >(e.g. similar to CSS code which can be put into a separate file) ?[/color]

                    See <URL:http://www.merlyn.demo n.co.uk/js-other.htm#Effy> , subsection
                    "Include :- *.js files".

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                    Comment

                    • Peter Connolly

                      #11
                      Re: How to outsource Javascript code from html code ?

                      Arthur Connor wrote:[color=blue]
                      > Is there a way of extracting the Javascript code from the "normal"
                      > HTML code (e.g. similar to CSS code which can be put into a separate
                      > file) ?
                      >
                      > If you offer a solution: can I determine in your solution where and
                      > which part of the code should be placed/inserted in the main HTML
                      > code?
                      >
                      > Sometimes a Javascript functionality is split up to several parts
                      > and/or
                      > it makes a big difference where the Javascript code should run/be
                      > called (e.g.
                      > - in the <HEAD>....</HEAD> or
                      > - at the beginning just behind the <BODY> tag
                      > - at the end just before the </BODY> tag.
                      > ....)
                      >
                      > Arty[/color]

                      Yes, you can do this.

                      Add a line in the head of your code such as the following;

                      <script language="JavaS cript" src="include/bfinclude.js"></script>

                      (this includes a javascript file which is located in the directory
                      'include')

                      Note that the above looks very similar to the existing markers for inline
                      javascript code; instead of having the routines listed, it uses the 'src'
                      parameter to locate a source file. The contents of the javascript file
                      should be just javascript, nothing else.

                      For example, the code below would be the entire content of the bfinclude.js
                      file (including the html comments)

                      <!--
                      function MM_reloadPage(i nit) { // Reloads the window if Nav4 resized
                      if (init==true) with (navigator) {if
                      ((appName=="Net scape")&&(parse Int(appVersion) ==4)) {
                      document.MM_pgW =innerWidth; document.MM_pgH =innerHeight;
                      onresize=MM_rel oadPage; }}
                      else if (innerWidth!=do cument.MM_pgW || innerHeight!=do cument.MM_pgH)
                      history.go(0);
                      }
                      -->

                      HTH,

                      Pete.
                      --

                      Peter Connolly | Macromedia Certified Dreamweaver MX Developer

                      Derby
                      UK



                      Comment

                      • HikksNotAtHome

                        #12
                        Re: How to outsource Javascript code from html code ?

                        In article <bsvn4f$8pq$1$8 300dec7@news.de mon.co.uk>, "Peter Connolly"
                        <noemailrequire d@nospamrequire d.com> writes:
                        [color=blue]
                        >Add a line in the head of your code such as the following;
                        >
                        ><script language="JavaS cript" src="include/bfinclude.js"></script>[/color]

                        It is wiser to use type="text/javascript" than it is language="javas cript" [1]
                        [color=blue]
                        >(this includes a javascript file which is located in the directory
                        >'include')
                        >
                        >Note that the above looks very similar to the existing markers for inline
                        >javascript code; instead of having the routines listed, it uses the 'src'
                        >parameter to locate a source file. The contents of the javascript file
                        >should be just javascript, nothing else.
                        >
                        >For example, the code below would be the entire content of the bfinclude.js
                        >file (including the html comments)[/color]

                        1) The "html comments" are not needed in any browser less than 10 years old.
                        2) They are not "html comments", they are part of the script block and as such
                        are part of the script.


                        [color=blue]
                        ><!--
                        >function MM_reloadPage(i nit) { // Reloads the window if Nav4 resized
                        > if (init==true) with (navigator) {if
                        >((appName=="Ne tscape")&&(pars eInt(appVersion )==4)) {
                        > document.MM_pgW =innerWidth; document.MM_pgH =innerHeight;
                        >onresize=MM_re loadPage; }}
                        > else if (innerWidth!=do cument.MM_pgW || innerHeight!=do cument.MM_pgH)
                        >history.go(0 );
                        >}
                        >-->
                        >[/color]

                        [1] Now I remember why I don't use Dreamweaver, I prefer reliable code.

                        See the FAQ with regards to object detection.
                        --
                        Randy

                        Comment

                        • Peter Connolly

                          #13
                          Re: How to outsource Javascript code from html code ?

                          HikksNotAtHome wrote:
                          [color=blue]
                          > It is wiser to use type="text/javascript" than it is
                          > language="javas cript" [1]
                          >
                          > 1) The "html comments" are not needed in any browser less than 10
                          > years old. 2) They are not "html comments", they are part of the
                          > script block and as such are part of the script.
                          >[/color]

                          Fair comment, and good advice. Thanks.
                          [color=blue]
                          >
                          > See the FAQ with regards to object detection.[/color]

                          Thanks, but I do very little with Javascript, so I'm not staying around! I
                          only dropped in to see where my response had disappeared to, as it didn't
                          show up in the newsgroup I had replied to; the OP had cross-posted, but I
                          didn't notice at the time - I normally remove cross-postings before
                          replying.

                          Regards,

                          Pete.
                          --

                          Peter Connolly | Macromedia Certified Dreamweaver MX Developer

                          Derby
                          UK



                          Comment

                          • Greg Griffiths

                            #14
                            Re: How to outsource Javascript code from html code ?

                            You certainly can, its called a .JS file - see
                            http://www.javascript-fx.com/general...aljs/help.html or search the web
                            for more info.

                            Arthur Connor wrote:
                            [color=blue]
                            > Is there a way of extracting the Javascript code from the "normal" HTML code
                            > (e.g. similar to CSS code which can be put into a separate file) ?
                            >
                            > If you offer a solution: can I determine in your solution where and which part
                            > of the code should be placed/inserted in the main HTML code?
                            >
                            > Sometimes a Javascript functionality is split up to several parts and/or
                            > it makes a big difference where the Javascript code should run/be called
                            > (e.g.
                            > - in the <HEAD>....</HEAD> or
                            > - at the beginning just behind the <BODY> tag
                            > - at the end just before the </BODY> tag.
                            > ....)
                            >
                            > Arty[/color]

                            Comment

                            • Dr John Stockton

                              #15
                              How to outsource Javascript code from html code ?

                              JRS: In article <20031231185727 .07578.00002376 @mb-m25.aol.com>, seen in
                              news:comp.lang. javascript, HikksNotAtHome <hikksnotathome @aol.com>
                              posted at Wed, 31 Dec 2003 23:57:27 :-[color=blue]
                              >In article <bsvn4f$8pq$1$8 300dec7@news.de mon.co.uk>, "Peter Connolly"
                              ><noemailrequir ed@nospamrequir ed.com> writes:[/color]
                              [color=blue][color=green]
                              >>For example, the code below would be the entire content of the bfinclude.js
                              >>file (including the html comments)[/color]
                              >
                              >1) The "html comments" are not needed in any browser less than 10 years old.
                              >2) They are not "html comments", they are part of the script block and as such
                              >are part of the script.[/color]

                              A) They may be useful if the page is being processed by software other
                              than a browser.

                              B) They are HTML comments, since they are for use when the script tag is
                              not recognised.

                              [color=blue][color=green]
                              >><!--
                              >>function MM_reloadPage(i nit) { // Reloads the window if Nav4 resized
                              >> if (init==true) with (navigator) {if[/color][/color]
                              if (init) ... should suffice.

                              --
                              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                              <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                              Comment

                              Working...