Wait till image is loaded from "onbeforeunload" handler?

Discussion in 'Javascript' started by Robert Oschler, Aug 30, 2003.

  1. 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/
    Robert Oschler, Aug 30, 2003
    #1
    1. Advertising

  2. "Robert Oschler" <no_replies@fake_email_address.invalid> wrote in
    message news:nU24b.20353$...
    >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.
    Richard Cornford, Aug 30, 2003
    #2
    1. Advertising

  3. Robert Oschler

    Grant Wagner Guest

    Robert Oschler wrote:

    > 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 <>

    * 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
    Grant Wagner, Sep 3, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,321
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. thehitman
    Replies:
    0
    Views:
    1,681
    thehitman
    Feb 2, 2006
  3. Udo Corban
    Replies:
    0
    Views:
    3,839
    Udo Corban
    Jan 23, 2004
  4. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,958
    -berlin.de
    Mar 1, 2004
  5. senges
    Replies:
    3
    Views:
    293
    Ingo R. Homann
    May 10, 2007
Loading...

Share This Page