469,364 Members | 2,332 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Aborting an ongoing Javascript form submission.

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

Nov 7 '06 #1
3 1881
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
Nov 7 '06 #2

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.

Nov 9 '06 #3
VK

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>

Nov 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.