Including external javascript code in a web page

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

    Including external javascript code in a web page

    In html, one can say <script language="JavaS cript"
    src="http://someCodeSnippet .js">

    Is there any way to embed the included code snippet in a function (in the
    case where it is not already a function in the remote souce file)?

    - David D.




  • McKirahan

    #2
    Re: Including external javascript code in a web page

    "David D." <daviddiamond.r emove-if-not-spam@comcast.ne t> wrote in message
    news:_tqdnbSkx6 nqHTbcRVn-tA@comcast.com. ..[color=blue]
    > In html, one can say <script language="JavaS cript"
    > src="http://someCodeSnippet .js">
    >
    > Is there any way to embed the included code snippet in a function (in the
    > case where it is not already a function in the remote souce file)?
    >
    > - David D.[/color]

    More properly:

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

    or

    <script type="text/javascript"
    src="http://{domain}/{folder}/someCodeSnippet .js"></script>


    Could you be more explicit regarding "embed the included code snippet in a
    function"?


    Comment

    • David D.

      #3
      Re: Including external javascript code in a web page

      "McKirahan" <News@McKirahan .com> wrote in message
      news:JlMqd.6887 47$8_6.455375@a ttbi_s04...[color=blue]
      >
      > Could you be more explicit regarding "embed the included code snippet in a
      > function"?
      >[/color]

      Let's say the referenced file, from a news service, contains the following
      two lines of code:

      document.write( '<br><a href="worldnews .com/article0391.htm l">New Casualties
      in Iraq</a>');
      document.write( '<br><a href="worldnews .com/article0402.htm l">Palestine Peace
      Overture</a>');

      This code is meant to be copied, in-line, into your web page, using the
      <script ... src="..."> syntax.

      However, there is a timing problem. The first time the web page is brought
      up after a fresh boot, the document.writes are not executed. Thereafter,
      when the files are cached in RAM, the web page displays fine. (I have tried
      it with I.E., Netscape, and Mozilla).

      As a workaround, I want to try creating a function in the page header:

      function getNews() {
      document.write( '<a href="worldnews .com/article0391.htm l">New Casualties
      in Iraq</a>');
      document.write( '<a href="worldnews .com/article0402.htm l">Palestine Peace
      Overture</a>');
      }

      In line, at the point in the page where I would want the links displayed, I
      would call the function.

      However, I have been unsuccessful in trying to create that function in the
      header, using the dynamic links from the referenced file.

      - David



      Comment

      • RobB

        #4
        Re: Including external javascript code in a web page

        "David D." <daviddiamond.r emove-if-not-spam@comcast.ne t> wrote in message news:<KsSdndxkc PFzRzbcRVn-sg@comcast.com> ...[color=blue]
        > "McKirahan" <News@McKirahan .com> wrote in message
        > news:JlMqd.6887 47$8_6.455375@a ttbi_s04...[color=green]
        > >
        > > Could you be more explicit regarding "embed the included code snippet in a
        > > function"?
        > >[/color]
        >
        > Let's say the referenced file, from a news service, contains the following
        > two lines of code:
        >
        > document.write( '<br><a href="worldnews .com/article0391.htm l">New Casualties
        > in Iraq</a>');
        > document.write( '<br><a href="worldnews .com/article0402.htm l">Palestine Peace
        > Overture</a>');
        >
        > This code is meant to be copied, in-line, into your web page, using the
        > <script ... src="..."> syntax.
        >
        > However, there is a timing problem. The first time the web page is brought
        > up after a fresh boot, the document.writes are not executed. Thereafter,
        > when the files are cached in RAM, the web page displays fine. (I have tried
        > it with I.E., Netscape, and Mozilla).
        >
        > As a workaround, I want to try creating a function in the page header:
        >
        > function getNews() {
        > document.write( '<a href="worldnews .com/article0391.htm l">New Casualties
        > in Iraq</a>');
        > document.write( '<a href="worldnews .com/article0402.htm l">Palestine Peace
        > Overture</a>');
        > }
        >
        > In line, at the point in the page where I would want the links displayed, I
        > would call the function.
        >
        > However, I have been unsuccessful in trying to create that function in the
        > header, using the dynamic links from the referenced file.
        >
        > - David[/color]

        Generally speaking, unlike HTML objects (like images, Flash, and
        Java), which can be requested in parallel, the HTML parser must wait
        for the JavaScript interpreter to load and execute any JavaScript
        files before it can continue. So this shouldn't be an issue. Where are
        you embedding the <script> tags?

        Comment

        • David D.

          #5
          Re: Including external javascript code in a web page

          The news links are in a table cell.

          It is actually my brother's web site, www.somerton.net. I am trying to
          help him debug the problem.

          If you are able to reproduce the problem, then the first time you load the
          page, you will see "World News" in the right hand column, but no news links.
          If you click the "Refresh" button, then the news links will appear. The
          problem will have gone away until your next cold boot.

          - David D.



          "RobB" <ferndoc9@hotma il.com> wrote in message
          news:ab354593.0 411300048.6bef5 72c@posting.goo gle.com...[color=blue]
          > "David D." <daviddiamond.r emove-if-not-spam@comcast.ne t> wrote in message[/color]
          news:<KsSdndxkc PFzRzbcRVn-sg@comcast.com> ...[color=blue][color=green]
          > > "McKirahan" <News@McKirahan .com> wrote in message
          > > news:JlMqd.6887 47$8_6.455375@a ttbi_s04...[color=darkred]
          > > >
          > > > Could you be more explicit regarding "embed the included code snippet[/color][/color][/color]
          in a[color=blue][color=green][color=darkred]
          > > > function"?
          > > >[/color]
          > >
          > > Let's say the referenced file, from a news service, contains the[/color][/color]
          following[color=blue][color=green]
          > > two lines of code:
          > >
          > > document.write( '<br><a href="worldnews .com/article0391.htm l">New[/color][/color]
          Casualties[color=blue][color=green]
          > > in Iraq</a>');
          > > document.write( '<br><a href="worldnews .com/article0402.htm l">Palestine[/color][/color]
          Peace[color=blue][color=green]
          > > Overture</a>');
          > >
          > > This code is meant to be copied, in-line, into your web page, using the
          > > <script ... src="..."> syntax.
          > >
          > > However, there is a timing problem. The first time the web page is[/color][/color]
          brought[color=blue][color=green]
          > > up after a fresh boot, the document.writes are not executed.[/color][/color]
          Thereafter,[color=blue][color=green]
          > > when the files are cached in RAM, the web page displays fine. (I have[/color][/color]
          tried[color=blue][color=green]
          > > it with I.E., Netscape, and Mozilla).
          > >
          > > As a workaround, I want to try creating a function in the page header:
          > >
          > > function getNews() {
          > > document.write( '<a href="worldnews .com/article0391.htm l">New[/color][/color]
          Casualties[color=blue][color=green]
          > > in Iraq</a>');
          > > document.write( '<a href="worldnews .com/article0402.htm l">Palestine[/color][/color]
          Peace[color=blue][color=green]
          > > Overture</a>');
          > > }
          > >
          > > In line, at the point in the page where I would want the links[/color][/color]
          displayed, I[color=blue][color=green]
          > > would call the function.
          > >
          > > However, I have been unsuccessful in trying to create that function in[/color][/color]
          the[color=blue][color=green]
          > > header, using the dynamic links from the referenced file.
          > >
          > > - David[/color]
          >
          > Generally speaking, unlike HTML objects (like images, Flash, and
          > Java), which can be requested in parallel, the HTML parser must wait
          > for the JavaScript interpreter to load and execute any JavaScript
          > files before it can continue. So this shouldn't be an issue. Where are
          > you embedding the <script> tags?[/color]


          Comment

          • .

            #6
            Re: Including external javascript code in a web page

            David D. wrote:[color=blue]
            > In html, one can say <script language="JavaS cript"
            > src="http://someCodeSnippet .js">
            >
            > Is there any way to embed the included code snippet in a function (in the
            > case where it is not already a function in the remote souce file)?
            >
            > - David D.
            >
            >
            >
            >[/color]
            This is the exact question I have. Unfortunately, perusing the billion
            messages this list generates, it's been asked several times in the past,
            with no answers. So I'm guessing: no.

            EXPERTS! Please prove me wrong!




            The closest I can come is something like::
            document.write( "<script src='stupid.js' ><" + "/script>"); /* NOTE
            the break is need to prevent it from closing your script prematurely.*/

            This will not run the external js until after the </script> tag is
            closed. But it will run it before any further html or script tags.

            So, My situation is: I have a parsing <script> that parses out info
            from the URL. it decides which external JS file to doc.write(). this
            file does not load until the parsing script closes. Then! I open another
            <script> which uses the variables from the external JS.

            It's a mess. It's ugly. I'm not even sure if it will work 100%,(works in
            IE6 and Moz1.4) but that's the closest I can get, short of learning PHP...

            Phil

            spamfilter@happ yINVALIDmagpie. com (to email, the CAPITAL LETTERS)






            Comment

            • Richard Cornford

              #7
              Re: Including external javascript code in a web page

              .. wrote:
              <snip>[color=blue][color=green]
              >> Is there any way to embed the included code snippet in a
              >> function (in the case where it is not already a function
              >> in the remote souce file)?[/color][/color]
              <snip>[color=blue]
              > This is the exact question I have. Unfortunately, perusing
              > the billion messages this list generates, it's been asked
              > several times in the past, with no answers. So I'm guessing:
              > no.[/color]
              <snip>

              While it may not be possible to insert the code imported by an external
              script in a function so that its execution can be deferred, it would be
              possible to defer any document.write calls that such code may use.

              First copy the real document.write mehtod to another property of the
              document object (to preserve its - this - reference when executed):-

              document.oldWri te = document.write;

              Then create an array in a suitable containing scope (global here) to
              contain anything written:-

              var stuffThatWasWri tten = [];

              Then replace the document.write method with one that will just copy its
              arguments to that array:-

              document.write = function(){
              for(var c = 0;c < arguments.lengt h;c++){
              stuffThatWasWri tten[stuffThatWasWri tten.length] = arguments[c];
              }
              }

              Now import the external script and instead of writing to the document it
              will just store strings in the array.

              Then resort the document.write method (or use the copy of it) and write
              out whatever is in the array at any point you choose:-

              document.oldWri te(stuffThatWas Written.join('' ));

              -or:-

              document.write = document.oldWri te;
              document.write( stuffThatWasWri tten.join(''));

              (but don't forget the document.writel n method)
              [color=blue]
              > The closest I can come is something like::
              > document.write( "<script src='stupid.js' ><" + "/script>");
              > /* NOTE the break is need to prevent
              > it from closing your script prematurely.*/[/color]

              The concatenation is not needed. Only disrupting the '</' character
              sequence in the source code is required and can be achieved by escaping
              the forward slash, resulting in '<\/'. And avoiding the concatenation is
              fractionally more efficient.

              Richard.


              Comment

              • .

                #8
                Re: Including external javascript code in a web page

                Richard Cornford wrote:

                <snip>[color=blue]
                >
                > Then replace the document.write method with one that will just copy its
                > arguments to that array:-
                >
                > document.write = function(){
                > for(var c = 0;c < arguments.lengt h;c++){
                > stuffThatWasWri tten[stuffThatWasWri tten.length] = arguments[c];
                > }
                > }
                >[/color]

                Huh. I didn't realise could overwrite some of the default functions.
                That would realy confuse the heck out of me if I did that accidentally...

                <snip>
                [color=blue][color=green]
                >>The closest I can come is something like::
                >>document.writ e("<script src='stupid.js' ><" + "/script>");
                >>/* NOTE the break is need to prevent
                >>it from closing your script prematurely.*/[/color]
                >
                >
                > The concatenation is not needed. Only disrupting the '</' character
                > sequence in the source code is required and can be achieved by escaping
                > the forward slash, resulting in '<\/'. And avoiding the concatenation is
                > fractionally more efficient.
                >[/color]

                I really should have remembered the escape characters. I only play with
                Javascript when I sporadically tweak my website, so I always seem to
                have to relearn everything each time...
                [color=blue]
                > Richard.
                >
                >[/color]
                Thanks, hopefully this helped the original poster as well.

                Phil

                Comment

                • Grant Wagner

                  #9
                  Re: Including external javascript code in a web page

                  "." wrote:
                  [color=blue]
                  > Richard Cornford wrote:
                  >
                  > <snip>[color=green]
                  > >
                  > > Then replace the document.write method with one that will just copy its
                  > > arguments to that array:-
                  > >
                  > > document.write = function(){
                  > > for(var c = 0;c < arguments.lengt h;c++){
                  > > stuffThatWasWri tten[stuffThatWasWri tten.length] = arguments[c];
                  > > }
                  > > }
                  > >[/color]
                  >
                  > Huh. I didn't realise could overwrite some of the default functions.
                  > That would realy confuse the heck out of me if I did that accidentally...[/color]

                  And many people do do it accidentally:

                  <form name="myForm">
                  <input type="hidden" name="function" value="">
                  <input type="submit" name="submit" value="Save record">
                  <input type="button" name="delete" value="Delete record"
                  onclick="
                  this.form.eleme nts['function'].value='delete' ;
                  this.form.submi t(); /* why doesn't this work? */
                  ">
                  </form>

                  --
                  Grant Wagner <gwagner@agrico reunited.com>
                  comp.lang.javas cript FAQ - http://jibbering.com/faq

                  Comment

                  Working...