Aborting an ongoing Javascript form submission.

Discussion in 'Javascript' started by Niall, Nov 7, 2006.

  1. Niall

    Niall Guest

    The short version: how does one get Javascript to abort a form
    submission/page load half way through?


    Long version:

    I have a page, the guts of which can be summarised as:


    <script>
    function poll_response() {
    // do a bunch of dhtml taking the contents of a cookie and inserting
    them into a div.

    window.setTimeout('abort_load()', 15000);

    document.forms['resp_poll_form'].submit();
    }

    function abort_load() {
    // um... what do we do here?
    }
    </script>
    </head>
    <body onLoad="poll_response(); setInterval('poll_response()', 1000);">
    <form name="resp_poll_form" action="/" method="POST">
    <!-- and so on... -->


    The (php) script that the form posts to puts information into the
    relevant cookie and sends an HTTP 204 response, so the actual page is
    undisturbed.

    Under ideal conditions, this means that the script will poll the server
    once per second, adding any new information to the page without
    actually reloading the page.

    However, occasionally the server will be unavailable. If the browser is
    allowed to continue attempting to read the response, it will eventually
    time out and stop the script. This is not the desired result; the
    browser should keep trying until the server becomes available again
    (which it always does, after a minute or so).

    Hence the setTimeout() call. The idea is that after 15 seconds of not
    hearing anything back from the server, the browser stops attempting the
    current transfer, and tries a new one.

    The problem is, I have no idea how to achieve this end. I was initally
    under the impression that the abort() function would, in Internet
    Explorer at least, cause any current data transfers to stop. As it
    turns out this isn't exactly the case: IE throws an error when it
    reaches that line, which *does* have the effect of stopping the
    transfer and allowing a new one to start, but it's not exactly clean.
    Firefox throws an error as well, but it also doesn't run any more
    scripts, so it doesn't work at all.

    So how does one cause the transfer to stop?

    I need solutions for this problem which will work in Internet Explorer
    >= 6.0 and Firefox >= 1.5. (Preferrably the *same* solution, but I don't hold out much hope...) Can anyone help?


    Thanks in advance,

    Niall
     
    Niall, Nov 7, 2006
    #1
    1. Advertising

  2. Niall

    Erwin Moller Guest

    Niall wrote:

    > The short version: how does one get Javascript to abort a form
    > submission/page load half way through?


    Sorry, I ahve been so bold not to read the whole question, but this can
    help:
    Start your page (in HEAD) with
    var pageLoaded = false;

    And at the very end of the page:
    var pageLoaded = true;

    Add to the form:
    <form .... onSubmit="return pageLoaded;">

    If onSubmit returns false, the form is not submitted, if true it is
    submitted.

    Hope that helps.

    Regards,
    Erwin Moller
     
    Erwin Moller, Nov 7, 2006
    #2
    1. Advertising

  3. Niall

    Niall Guest

    Erwin Moller wrote:

    > Sorry, I ahve been so bold not to read the whole question


    You should have; the answer you gave is unrelated to the question.
     
    Niall, Nov 9, 2006
    #3
  4. Niall

    VK Guest

    Niall wrote:
    > The short version: how does one get Javascript to abort a form
    > submission/page load half way through?


    Long sad answer (but see the happy one at the bottom): you can't. Start
    of form submission means end of the current page existence. Despite
    "orphan script" may remain in the execution stack for some time, its
    usage would be the last option to choose for a reliable solution.

    Morover you have only partial control over UA timeout: it has its own
    period in seconds defined in system settings (10sec - 60sec regularly)
    so you can abort earlier - but not later.

    Short happy answer: there is IXMLHTTPRequest/XMLHttpRequest technique
    and that will work for you. See for instance
    <http://www.ajaxtoolbox.com>
     
    VK, Nov 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willem Oosthuizen

    Aborting Fucntions

    Willem Oosthuizen, Jul 10, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,051
  2. Lloyd Sheen

    OT: Ongoing story of Silverlight

    Lloyd Sheen, Mar 4, 2009, in forum: ASP .Net
    Replies:
    2
    Views:
    316
    Lloyd Sheen
    Mar 4, 2009
  3. Kenneth McDonald
    Replies:
    1
    Views:
    79
    Michael Guterl
    Oct 22, 2008
  4. Jared
    Replies:
    5
    Views:
    214
    Jared
    Jul 10, 2003
  5. at
    Replies:
    1
    Views:
    89
    Tad McClellan
    Dec 6, 2003
Loading...

Share This Page