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

Re: [JavaScript] Unable to submit form during onUnload

Expand Messages
  • Andy.T
    I think it may be a good idea to include the alert anyway as it alows the person using the form to know that their form has been submitted. (Otherwise they may
    Message 1 of 2 , Sep 30 12:40 PM
    • 0 Attachment
      I think it may be a good idea to include the alert anyway as it alows the
      person using the form to know that their form has been submitted. (Otherwise
      they may think they have not submitted anything.)

      Andy

      -------Original Message-------

      From: javascript_official@yahoogroups.com
      Date: Monday, September 29, 2003 16:41:29
      To: javascript_official@yahoogroups.com
      Subject: [JavaScript] Unable to submit form during onUnload

      Hi,

      I am trying to submit a form when the page is unloaded.
      The form is not submitted when the browser's refresh
      button is pressed. But if I add a alert statement after the
      submit statement, it works fine. I do not know what difference
      the alert statement makes. Is there any way where I can submit
      my form successfully without the alert statement?

      Please find my code below. I am using IE 6.0.


      This code doesn't work.


      <html>
      <head>
      <script language="Javascript">
      function myunload()
      {
      document.myform.submit();
      }

      </script>
      </head>
      <body onUnload="myunload()">
      My page.
      <form name="myform" action="servlet/TestServlet" method="POST">
      <input type="text" name="mytext" value="12" >

      </body>
      </html>


      This code works.

      <html>
      <head>
      <script language="Javascript">
      function myunload()
      {
      document.myform.submit();
      alert("Some message");
      }

      </script>
      </head>
      <body onUnload="myunload()">
      My page.
      <form name="myform" action="servlet/TestServlet" method="POST">
      <input type="text" name="mytext" value="12" >

      </body>
      </html>


      Servlet Code:

      import java.util.*;
      import java.io.*;

      import javax.servlet.*;
      import javax.servlet.http.*;

      public class TestServlet extends HttpServlet
      {

      public void doPost(HttpServletRequest req,HttpServletResponse res)
      {
      try
      {

      System.out.println("in dopost"+new Date().toString());
      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      res.setContentType("text/plain");
      out.println("Page submitted!!");
      }
      catch(Exception ex)
      {
      ex.printStackTrace();
      }

      }
      }

      Thanks,
      Karthik

      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.