undefined data in javascript

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

    undefined data in javascript

    I have no idea why id is undefined in the arrow below. Please advise. Thanks!!

    <html>
    <head>
    <script type="text/javascript">
    var id;
    function window_onload()
    {
    id = "8888";
    alert("ID = " + id);
    }
    </script>
    </head>
    <H2>Page 2</H2>
    <body onload="window_ onload()">
    <P>id = <script type="text/javascript">doc ument.write(id) ;</script> //<****
    </body>
    </html>
  • Randy Webb

    #2
    Re: undefined data in javascript

    Matt wrote:[color=blue]
    > I have no idea why id is undefined in the arrow below. Please advise. Thanks!!
    >
    > <html>
    > <head>
    > <script type="text/javascript">
    > var id;
    > function window_onload()
    > {
    > id = "8888";
    > alert("ID = " + id);
    > }
    > </script>
    > </head>
    > <H2>Page 2</H2>
    > <body onload="window_ onload()">
    > <P>id = <script type="text/javascript">doc ument.write(id) ;</script> //<****
    > </body>
    > </html>[/color]
    Because its value is not set until the onload event fires, which is
    *after* you try to write it, so its not set yet. So, you get undefined
    (which it is).



    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • mscir

      #3
      Re: undefined data in javascript


      Randy Webb wrote:[color=blue]
      > Matt wrote:
      >[color=green]
      >> I have no idea why id is undefined in the arrow below. Please advise.
      >> Thanks!!
      >>
      >> <html>
      >> <head>
      >> <script type="text/javascript">
      >> var id;
      >> function window_onload()
      >> {
      >> id = "8888";
      >> alert("ID = " + id);
      >> }
      >> </script>
      >> </head>
      >> <H2>Page 2</H2>
      >> <body onload="window_ onload()">
      >> <P>id = <script type="text/javascript">doc ument.write(id) ;</script>
      >> //<****
      >> </body>
      >> </html>[/color]
      >
      > Because its value is not set until the onload event fires, which is
      > *after* you try to write it, so its not set yet. So, you get undefined
      > (which it is).[/color]

      Yup, I tried this:

      <html>
      <head>
      <script type="text/javascript">
      var id;
      function window_onload() {
      //id = "8888";
      alert("ID = " + id);
      }
      </script>
      </head>

      <body onload="window_ onload()">
      <script type="text/javascript">
      var id="8888";
      </script>
      <form>
      <P>id = <script type="text/javascript">doc ument.write(id) ;</script>
      </form>
      </body>
      </html>

      Comment

      • Lee

        #4
        Re: undefined data in javascript

        Matt said:[color=blue]
        >
        >I have no idea why id is undefined in the arrow below. Please advise. Thanks!!
        >
        ><html>
        ><head>
        ><script type="text/javascript">
        >var id;
        >function window_onload()
        >{
        > id = "8888";
        > alert("ID = " + id);
        >}
        ></script>
        ></head>
        ><H2>Page 2</H2>
        ><body onload="window_ onload()">
        ><P>id = <script type="text/javascript">doc ument.write(id) ;</script> //<****
        ></body>
        ></html>[/color]

        id isn't defined until the onLoad event handler executes,
        but the inline script tries to write its value as the page is still loading.

        Comment

        • mscir

          #5
          Re: undefined data in javascript

          oops... forgot to clean it up first

          <html>
          <head>
          <script type="text/javascript">
          var id;
          function window_onload() {
          alert("ID = " + id);
          }
          </script>
          </head>

          <body onload="window_ onload()">
          <script type="text/javascript">
          id="8888";
          </script>
          <P>id = <script type="text/javascript">doc ument.write(id) ;</script>
          </body>
          </html>

          Comment

          Working...