XmlHttpRequest cancelled when the page is unloaded

P

perrog

Hi!

What is the expected behaviour when you send an XmlHttpRequest just
before the page is about to unload? I'm sending a XmlHttpRequest on an
onClick event, and I can inspect that the request is sent and
responded in the network traffic, but my onReadyStateChange handler is
notified with an error.

It took me some time to deduce what the real problem was, and I think
my browser begins to tear down the XmlHttpRequest when the page is
about to unload. Therefore, the request is sent, but the browser
cancels before the response is processed and onReadyStateChange is
notified with an error.

I'm using Safari/KHTML.

I searched W3's draft specification,
http://www.w3.org/TR/XMLHttpRequest/
but could not find any answer.

Should the browser wait on XmlHttpRequest completion before the page
is unload? Or should the browser cancel pending XmlHttpRequests when
the page is about to unload?
 
P

pcx99

Hi!

What is the expected behaviour when you send an XmlHttpRequest just
before the page is about to unload? I'm sending a XmlHttpRequest on an
onClick event, and I can inspect that the request is sent and
responded in the network traffic, but my onReadyStateChange handler is
notified with an error.

It took me some time to deduce what the real problem was, and I think
my browser begins to tear down the XmlHttpRequest when the page is
about to unload. Therefore, the request is sent, but the browser
cancels before the response is processed and onReadyStateChange is
notified with an error.

I'm using Safari/KHTML.

I searched W3's draft specification,
http://www.w3.org/TR/XMLHttpRequest/
but could not find any answer.

Should the browser wait on XmlHttpRequest completion before the page
is unload? Or should the browser cancel pending XmlHttpRequests when
the page is about to unload?

From my experience (which does not include Safari), a user's navigation
requests will trump anything save for modal dialog boxes (alert, prompt,
etc). That means, no matter what your program is doing, it will be
halted in its tracks when the user navigates away.

HOWEVER, a synchronous Ajax request is "modal" in that it will stall
your script until it receives an answer back from the server. I don't
know if it will interfere with user navigation requests the way a
prompt/alert box will, but if you're desperate you might want to try
using a synchronous Ajax request instead of an asynchronous request.
 
P

pcx99

pcx99 said:
From my experience (which does not include Safari), a user's navigation
requests will trump anything save for modal dialog boxes (alert, prompt,
etc). That means, no matter what your program is doing, it will be
halted in its tracks when the user navigates away.

HOWEVER, a synchronous Ajax request is "modal" in that it will stall
your script until it receives an answer back from the server. I don't
know if it will interfere with user navigation requests the way a
prompt/alert box will, but if you're desperate you might want to try
using a synchronous Ajax request instead of an asynchronous request.

And no sooner do I write this than I stumble across an article which
tests this very thesis.

http://www.oreillynet.com/xml/blog/2007/01/do_sync_calls_freeze_browsers.html

Firefox will stall, opera and IE do not stall, and Firefox considers the
stall a bug, or defect, and is fixed in the nightly builds which means
the next release of Firefox will not stall waiting for synchronous requests.

The article was dated only yesterday so I do have a little excuse ;)
 
L

Laurent Bugnion [MVP]

Hi,
And no sooner do I write this than I stumble across an article which
tests this very thesis.

http://www.oreillynet.com/xml/blog/2007/01/do_sync_calls_freeze_browsers.html


Firefox will stall, opera and IE do not stall, and Firefox considers the
stall a bug, or defect, and is fixed in the nightly builds which means
the next release of Firefox will not stall waiting for synchronous
requests.

The article was dated only yesterday so I do have a little excuse ;)

Careful here. The *browser* will not freeze, but the *current page* will
freeze. So actually, your first answer is totally correct IMHO, in that
a sync AJAX call behaves very much like an "alert", and the current
script execution is blocked until the response arrives (or until an
error occurs).

This is still a reason to recommend not using sync calls except in
special cases. Having the current page blocked for a noticeable time is
not user friendly, and can create unwanted side effects.

HTH,
Laurent
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top