Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / January 2007



Tip: Looking for answers? Try searching our database.

XmlHttpRequest cancelled when the page is unloaded

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
perrog@gmail.com - 29 Jan 2007 10:50 GMT
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?
pcx99 - 29 Jan 2007 21:36 GMT
> Hi!
>
[quoted text clipped - 19 lines]
> 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.

Signature

http://www.hunlock.com -- Musings in Javascript, CSS.
$FA

pcx99 - 30 Jan 2007 01:02 GMT
>  From my experience (which does not include Safari), a user's navigation
> requests will trump anything save for modal dialog boxes (alert, prompt,
[quoted text clipped - 6 lines]
> 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 ;)

Signature

http://www.hunlock.com -- Musings in Javascript, CSS.
$FA

Laurent Bugnion [MVP] - 31 Jan 2007 08:27 GMT
Hi,

>>  From my experience (which does not include Safari), a user's
>> navigation requests will trump anything save for modal dialog boxes
[quoted text clipped - 18 lines]
>
> 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
Signature

Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.