Why is array undefined???

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

    Why is array undefined???

    <html>
    <head>
    <title>Testin g JSP</title>

    <script type="text/javascript">

    var numbers = new Array("one", "two", "three", "four", "five");

    function test() {
    for(var i = 0; i < 5; i++) {
    document.writel n("It is: " + numbers[i]);
    }
    }

    </script>
    </head>
    <body onload="test()" >

    </body>
    </html>

    When i run this, it outputs "It is: one" and then complains that the
    varibale numbers is undefined???

    Why does it do this?

    Thanks

  • Janwillem Borleffs

    #2
    Re: Why is array undefined???

    Dave Casserly wrote:[color=blue]
    > When i run this, it outputs "It is: one" and then complains that the
    > varibale numbers is undefined???
    >[/color]

    Because you are using document.writel n after the document has been loaded,
    it's content gets overwritten, including the array definition.

    Move the call to test() to the body section, remove the onload event from
    the body tag and you will be fine.


    JW



    Comment

    • Grant Wagner

      #3
      Re: Why is array undefined???

      Dave Casserly wrote:
      [color=blue]
      > <html>
      > <head>
      > <title>Testin g JSP</title>
      >
      > <script type="text/javascript">
      >
      > var numbers = new Array("one", "two", "three", "four", "five");
      >
      > function test() {
      > for(var i = 0; i < 5; i++) {
      > document.writel n("It is: " + numbers[i]);
      > }
      > }
      >
      > </script>
      > </head>
      > <body onload="test()" >
      >
      > </body>
      > </html>
      >
      > When i run this, it outputs "It is: one" and then complains that the
      > varibale numbers is undefined???
      >
      > Why does it do this?
      >
      > Thanks[/color]

      Because after the script runs document.writel n() it wipes out the content of
      the current page, including everything between <script> and </script>

      You shouldn't document.write( ) or writeln() to a document that has completed
      loading and has been closed.

      <body>
      <script type="text/javascript">
      var numbers = new Array("one", "two", "three", "four", "five");
      function test() {
      for(var i = 0; i < 5; i++) {
      document.writel n("It is: " + numbers[i]);
      }
      }
      </script>
      </body>

      Will work. As will:

      <body>
      <script type="text/javascript">
      test();
      </script>
      </body>

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      Working...