JavaScript in HTML: unexpected outputs

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

    JavaScript in HTML: unexpected outputs

    [My initial posting didn't seem to make it, so here's retry]

    I'm learning/experimenting with some simple JS/html markup, running
    an apache daemon and mozilla firefox browser in RH 9.

    Let's say I run the following markup with one or more of lines 6-10
    commented out:

    1 <html>
    2 <head>
    3 <script type="text/javascript">
    4 function showinfo()
    5 {
    6 width = window.screen.a vailWidth;
    7 document.write( "<br>screen width=\n");
    8 document.write( width);
    9 document.write( "<br>Date: ");
    10 document.write( Date());
    11 }
    12 </script>
    13 </head>
    14 <body onload="showinf o()">
    15 <p>

    My question: is there a simple explanation to account for the 'unexpected'
    behaviours below (Runs 1, 3, 5)?

    Run 1: nothing in 6-10 commented out; output (expected more):

    screen width=

    Run 2: line 7 commented out; output (as expected):

    1024
    Date: Wed Apr 14 2004 20:55:58 GMT-0400 (EDT)

    Run 3: lines 7, 9, 10 commented out; output (as expected, but unfinished loading):

    1024
    [ongoing hourglass indicating loading in progress]

    Run 4: lines 7, 10 commented out; output (as expected):

    1024

    Run 5: lines 7-9 commented out; output (as expected, but unfished loading):

    Date: Wed Apr 14 2004 22:46:49 GMT-0400 (EDT)

    I'm sure I'm missing something obvious. Helpful comments would be much appreciated.

    --Tony
  • Richard Cornford

    #2
    Re: JavaScript in HTML: unexpected outputs

    Tony Gahlinger wrote:
    <snip>[color=blue]
    > 4 function showinfo()
    > 5 {
    > 6 width = window.screen.a vailWidth;
    > 7 document.write( "<br>screen width=\n");
    > 8 document.write( width);
    > 9 document.write( "<br>Date: ");
    > 10 document.write( Date());
    > 11 }
    > 12 </script>
    > 13 </head>
    > 14 <body onload="showinf o()">[/color]
    <snip>

    After a document has closed (which will have happened before the onload
    event) any call to - document.write - will clear and replace the current
    document with whatever is written. But the function - showinfo - is
    associated with the current document and is cleared, while it is
    executing. Unsurprisingly the consequences of removing the environment
    of a script while it is executing tend to be inconsistent and
    undesirable.

    If you want to call document.write after the current page has closed I
    would recommend outputting all of the new contents with just one call
    to - document.write - rather than several.

    Also, if a - document.open - call is made, or a - document.write - call
    is made on a document that is closed (which internally calls the open
    method when the document is not currently open), the document is not
    re-closed until the - document.close - method is called. Hence the
    impression that the page has not finished loading.

    Richard.


    Comment

    • Tony Gahlinger

      #3
      Re: JavaScript in HTML: unexpected outputs

      Richard Cornford wrote:[color=blue]
      > Tony Gahlinger wrote:
      > <snip>
      >[color=green]
      >> 4 function showinfo()
      >> 5 {
      >> 6 width = window.screen.a vailWidth;
      >> 7 document.write( "<br>screen width=\n");
      >> 8 document.write( width);
      >> 9 document.write( "<br>Date: ");
      >>10 document.write( Date());
      >>11 }
      >>12 </script>
      >>13 </head>
      >>14 <body onload="showinf o()">[/color]
      >
      > <snip>
      >
      > After a document has closed (which will have happened before the onload
      > event) any call to - document.write - will clear and replace the current
      > document with whatever is written. But the function - showinfo - is
      > associated with the current document and is cleared, while it is
      > executing. Unsurprisingly the consequences of removing the environment
      > of a script while it is executing tend to be inconsistent and
      > undesirable.
      >
      > If you want to call document.write after the current page has closed I
      > would recommend outputting all of the new contents with just one call
      > to - document.write - rather than several.
      >
      > Also, if a - document.open - call is made, or a - document.write - call
      > is made on a document that is closed (which internally calls the open
      > method when the document is not currently open), the document is not
      > re-closed until the - document.close - method is called. Hence the
      > impression that the page has not finished loading.
      >
      > Richard.[/color]

      Ah ... that makes sense all right; especially the reason for the unfinished
      loading (which one sees so often). Thanks!

      --Tony

      Comment

      Working...