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

Re: [JavaScript] Unable to submit form during onUnload

Expand Messages
  • RNC
    My understanding of onUnload is that it is performed immediately after unloading the page. Which would mean your form shouldn t exist anymore. But that
    Message 1 of 2 , Sep 29, 2003
    View Source
    • 0 Attachment
      My understanding of onUnload is that it is performed immediately
      after unloading the page. Which would mean your form shouldn't
      exist anymore. But that wouldn't explain why adding alert() would
      work.

      Personally, I would say you need to find a different approach.
      Maybe setting a flag to indicate if the form was submitted or not
      (clear flag when page loaded, set flag on submit). If onUnload see
      that flag is not set, it could reload the page. Haven't tried it
      but sounds like it might work to me.

      Regards,
      Rich

      > 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
      > --
      > ___________________________________________________
      > OperaMail free e-mail - http://www.operamail.com
      > OperaMail Premium - 28MB, POP3, more! US$29.99/year
      >
      > Powered by Outblaze
      >
      >
      >
      > To unsubscribe from this group, send an email to:
      > javascript_official-unsubscribe@yahoogroups.com
      >
      >
      >
      > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
      >
      >
      >
    • 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 2 of 2 , Sep 30, 2003
      View Source
      • 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.