Wait till image is loaded from "onbeforeunload" handler?

R

Robert Oschler

I have a web page where I want to make sure that an image HTTP request is
successfully made before the page unloads. The request is made by setting
the image element's SRC property to the desired URL.

When I put the SRC property change in either an "onbeforeunload" or
"onunload" event handler, the HTTP request is not received by our server as
much as 75% of the time.

I thought of setting a flag using an "onload" handler for the image element,
to tell me when the HTTP request had completed, and then waiting for that
flag in the "onbeforeunload" or "onunload" document event handlers. But
that doesn't work because Internet Explorer only processes one event at a
time.

Can anyone think of a reliable strategy to make sure that an HTTP request is
definitely sent off before leaving the page?

thx

--

Robert Oschler
"Let the web hear you, add your voice to your web site in minutes!"
-- http://audiodirect.spiderchase.com/
(For a limited time, free voiceover with every sign-up, use this link
instead)
-- http://audio.spiderchase.com/
(A song - are you blue?)
-- http://bluedreams.spiderchase.com/
 
R

Richard Cornford

Robert Oschler said:
I have a web page where I want to make sure that an image HTTP
request is successfully made before the page unloads. The request
is made by setting the image element's SRC property to the desired
URL.
When I put the SRC property change in either an
"onbeforeunload" or "onunload" event handler, the HTTP
request is not received by our server as much as 75% of the time.

I thought of setting a flag using an "onload" handler for the
image element, to tell me when the HTTP request had completed,
and then waiting for that flag in the "onbeforeunload" or "onunload"
document event handlers. But that doesn't work because Internet
Explorer only processes one event at a time.

Can anyone think of a reliable strategy to make sure that an HTTP
request is definitely sent off before leaving the page?

I doubt that your stated desire can be realistically achieved over the
Internet as there is nothing to guarantee that the user is connected at
the point - onbeforeunload - or - onunload - events are triggered (and
lots of other reasons on top). But you are apparently writing
exclusively for IE so your application may not be for the Internet. If
not you should say so.

However, It would probably be best to:
1. Show the code that is not working (otherwise obvious
improvements/suggestions cannot be made.
2. Explain why you want to do this. If it is something that needs to be
100% reliable then attempting it using JavaScript is probably trying to
solve the wrong problem.

Richard.
 
G

Grant Wagner

Robert said:
I have a web page where I want to make sure that an image HTTP request is
successfully made before the page unloads. The request is made by setting
the image element's SRC property to the desired URL.

When I put the SRC property change in either an "onbeforeunload" or
"onunload" event handler, the HTTP request is not received by our server as
much as 75% of the time.

I thought of setting a flag using an "onload" handler for the image element,
to tell me when the HTTP request had completed, and then waiting for that
flag in the "onbeforeunload" or "onunload" document event handlers. But
that doesn't work because Internet Explorer only processes one event at a
time.

Can anyone think of a reliable strategy to make sure that an HTTP request is
definitely sent off before leaving the page?

thx

No, you can't guarantee anything done with JavaScript executes at any time. And
relying on it to close database connections or log the user off or whatever you
are relying on it for is a flawed design.

Design the server-side processes to handle timed out sessions correctly and
rationally and then you don't need to rely on client-side technologies to do
what they cannot do.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top