Aborting an ongoing Javascript form submission.

N

Niall

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
 
E

Erwin Moller

Niall said:
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
 
V

VK

Niall said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top