Loading ...
Sorry, an error occurred while loading the content.

Why doesn't this work?

Expand Messages
  • labroid
    I am a javascript newbie, and apparently don t understand the scope of the DOM objects (which I thought were global?). Please look at the javascript and its
    Message 1 of 2 , Jan 2, 2005
    • 0 Attachment
      I am a javascript newbie, and apparently don't understand the scope of
      the DOM objects (which I thought were global?). Please look at the
      javascript and its associated output below. I would like to know whey
      the number of forms is visible outside of the javascript function, but
      is not visible inside the function. I need to know form information
      inside the function in the <head> section, and I would appreciate any
      information that might help me understand this.

      Thanks!

      ------------Sample Code-------------
      <html>
      <head>
      <title>Untitled</title>
      <script type="text/javascript">
      <!--
      function SubmitForm() {
      document.write("Number of forms from within function: ")
      document.write(document.forms.length)
      }
      // -->
      </script>
      </head>
      <body>
      <script type="text/javascript">
      <!--
      document.write("<form action=\"#\">");
      document.write("<input>");
      document.write("</form>");
      document.write("<form action=\"#\">");
      document.write("<input>");
      document.write("</form>");
      document.write(" <button name=\"SubmitButton\" type=\"button\"
      onmouseup=\"SubmitForm(document)\" >Submit</button>");
      document.write("<br />Number of forms from outside function: " +
      document.forms.length)
      // -->
      </script>
      </body>
      </html>

      ----------------------OUTPUT-----------------

      [input box graphic]
      [input box graphic]
      Number of forms from outside function: 2
      [button graphic]

      -------------Then when I click the button---------

      Number of forms from within function: 0
    • Jon Stephens
      The problem is that, once the page has loaded, calling document.write() again destroys everything in the page. (Notice how the forms disappeared when you
      Message 2 of 2 , Jan 3, 2005
      • 0 Attachment
        The problem is that, once the page has loaded, calling document.write()
        again destroys everything in the page. (Notice how the forms disappeared
        when you clicked the button? That's because they did in fact disappear.)

        The DOM collections are global in scope (as are all properties and
        methods of document):

        <html>
        <head>
        <title>Untitled</title>
        <script type="text/javascript">
        function report()
        {
        var output = "Number of forms from within function: ";
        alert(output + document.forms.length);
        }
        </script>
        </head>
        <body>
        <script type="text/javascript">
        document.write("<form action=\"#\">");
        document.write("<input>");
        document.write("</form>");
        document.write("<form action=\"#\">");
        document.write("<input>");
        document.write("</form>");
        document.write("<button name=\"SubmitButton\" type=\"button\"
        onclick=\"report();\">Report</button>");
        document.write("<br />Number of forms from outside function: " +
        document.forms.length);
        </script>
        </body>
        </html>

        > Date: Sun, 02 Jan 2005 15:24:20 -0000
        > From: "labroid" <labroid@...>
        > Subject: Why doesn't this work?
        >
        >
        >
        > I am a javascript newbie, and apparently don't understand the scope of
        > the DOM objects (which I thought were global?). Please look at the
        > javascript and its associated output below. I would like to know whey
        > the number of forms is visible outside of the javascript function, but
        > is not visible inside the function. I need to know form information
        > inside the function in the <head> section, and I would appreciate any
        > information that might help me understand this.
        >
        > Thanks!
        >
        > ------------Sample Code-------------
        > <html>
        > <head>
        > <title>Untitled</title>
        > <script type="text/javascript">
        > <!--
        > function SubmitForm() {
        > document.write("Number of forms from within function: ")
        > document.write(document.forms.length)
        > }
        > // -->
        > </script>
        > </head>
        > <body>
        > <script type="text/javascript">
        > <!--
        > document.write("<form action=\"#\">");
        > document.write("<input>");
        > document.write("</form>");
        > document.write("<form action=\"#\">");
        > document.write("<input>");
        > document.write("</form>");
        > document.write(" <button name=\"SubmitButton\" type=\"button\"
        > onmouseup=\"SubmitForm(document)\" >Submit</button>");
        > document.write("<br />Number of forms from outside function: " +
        > document.forms.length)
        > // -->
        > </script>
        > </body>
        > </html>
        >
        > ----------------------OUTPUT-----------------
        >
        > [input box graphic]
        > [input box graphic]
        > Number of forms from outside function: 2
        > [button graphic]
        >
        > -------------Then when I click the button---------
        >
        > Number of forms from within function: 0
        >
        >
        >
        >
        >
        >
        >
        >
        > ________________________________________________________________________




        --
        No virus found in this outgoing message.
        Checked by AVG Anti-Virus.
        Version: 7.0.300 / Virus Database: 265.6.8 - Release Date: 2005-01-03
      Your message has been successfully submitted and would be delivered to recipients shortly.