Urgent!!!: Onunload event (refreshing page), can not redirect (window.location.href does not work)

Discussion in 'Javascript' started by Jiong Feng, Jun 23, 2004.

  1. Jiong Feng

    Jiong Feng Guest

    Hi, (I only consider IE6)

    I am writing a page which redirect user to another page during onunloadevent
    (I will use window.confirm to ask user's agreement).

    But when user do a refresh, the window.confirm shows up but if user select
    ok, the redirect fails.

    How to make it work? (either not show window.confirm on refresh window, or
    make the redirect work on refresh)

    Thanks
    Jiong

    Sample code is here:
    ////////////////////////////////////////////////////////////////////////////
    ///////////////////////////
    <html>
    <script language="Javascript">
    function onUnLoadHandler() {
    if(window.confirm("agree to redirect?"))
    window.location.href="/redirect.htm"; // this does not work
    when refresh window, why?
    }
    </script>
    <body OnUnload="onUnLoadHandler()" >
    something
    </body>
    </html>
    ////////////////////////////////////////////////////////////////////////////
    //////////////////////////
    Jiong Feng, Jun 23, 2004
    #1
    1. Advertising

  2. First you gives an alert in your handler, if alerted, the handler works. In
    this case you need only replace the relative path with absolute path.

    Otherwise, post here again.
    CryingClinton, Jun 23, 2004
    #2
    1. Advertising

  3. Jiong Feng

    Jiong Feng Guest

    I am not sure what you mean about "alert", how to do as you said?

    I also tried to use absolute URL but seems it does not work either.

    Thanks
    Jiong

    "CryingClinton" <> wrote in message
    news:cbav04$2gtt$99.com...
    > First you gives an alert in your handler, if alerted, the handler works.

    In
    > this case you need only replace the relative path with absolute path.
    >
    > Otherwise, post here again.
    >
    >
    Jiong Feng, Jun 23, 2004
    #3
  4. <script language="Javascript">
    function onUnLoadHandler() {
    alert('Handler runs!');
    }
    </script>


    "Jiong Feng" <> дÈëÓʼþ
    news:...
    > I am not sure what you mean about "alert", how to do as you said?
    >
    > I also tried to use absolute URL but seems it does not work either.
    >
    > Thanks
    > Jiong
    CryingClinton, Jun 23, 2004
    #4
  5. Jiong Feng

    Jiong Feng Guest

    Oh, but as my window.confirm dialog shows up, I am sure the event is called.


    "CryingClinton" <> wrote in message
    news:cbb77f$2jrg$99.com...
    > <script language="Javascript">
    > function onUnLoadHandler() {
    > alert('Handler runs!');
    > }
    > </script>
    >
    >
    > "Jiong Feng" <> дÈëÓʼþ
    > news:...
    > > I am not sure what you mean about "alert", how to do as you said?
    > >
    > > I also tried to use absolute URL but seems it does not work either.
    > >
    > > Thanks
    > > Jiong

    >
    >
    Jiong Feng, Jun 23, 2004
    #5
  6. before set new location, give a false value to the event returnValue in your
    handler.

    window.event.returnValue = false;
    window.event.cancelBubble = true;//stop the event being passed to higher
    level

    window.location=... //OR window.navigate(...); //go to new location

    "Jiong Feng" <> дÈëÓʼþ
    news:40d92601$...
    > Oh, but as my window.confirm dialog shows up, I am sure the event is

    called.
    CryingClinton, Jun 23, 2004
    #6
  7. Note that navigating to the new location will also fire the 'onunload'
    event. Be sure your handler considered this, or you may get infinite loop.
    CryingClinton, Jun 23, 2004
    #7
  8. Re: Urgent!!!: Onunload event (refreshing page), can not redirect(window.location.href does not work)

    "CryingClinton" <> writes:

    > before set new location, give a false value to the event returnValue in your
    > handler.
    >
    > window.event.returnValue = false;
    > window.event.cancelBubble = true;//stop the event being passed to higher
    > level


    Both of these are IE specific. The DOM methods would be:
    evt.prevenDefault();
    and
    evt.stopPropagation();
    where "evt" was the argument to the handler function.


    My handler usually end up looking like:
    ---
    function handleSomething(event) {
    event = event || window.event; // IE sucks!
    var tgt = event.target || event.srcElement;
    // ...
    if (event.stopPropagation) {
    event.stopPropagation();
    } else {
    event.cancelBubble = true;
    }

    if (event.preventDefault) {
    event.preventDefault();
    } else {
    event.returnValue = false;
    }
    return false;
    }
    ---
    That covers most cases.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jun 23, 2004
    #8
  9. > Both of these are IE specific. The DOM methods would be:
    > evt.prevenDefault();
    > and
    > evt.stopPropagation();
    > where "evt" was the argument to the handler function.
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors:

    <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'


    Man, you hate M$ the most!
    hehe... :)
    CryingClinton, Jun 24, 2004
    #9
  10. Re: Urgent!!!: Onunload event (refreshing page), can not redirect(window.location.href does not work)

    "CryingClinton" <> writes:

    > Man, you hate M$ the most!


    I don't *hate* Microsoft.
    Well, maybe a little.
    Ok, I do.

    But my main problem is that I don't use IE, so when I see code that
    only works in IE being suggested as a solution to a problem, I can
    easily envision myself trying to use the resulting page ... and fail.

    Instead of swearing at the page author at that time, I prefer to swear
    at Microsoft now. If the page author *still* makes a page I can't use,
    I won't even have a bad conscience swearing at him later :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jun 24, 2004
    #10
    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. AAaron123
    Replies:
    3
    Views:
    7,697
    AAaron123
    May 1, 2009
  2. Andoni
    Replies:
    3
    Views:
    110
    Richard Cornford
    Sep 26, 2003
  3. saiho.yuen
    Replies:
    3
    Views:
    421
    kaeli
    Sep 14, 2004
  4. Replies:
    2
    Views:
    462
  5. alison
    Replies:
    3
    Views:
    405
    Thomas 'PointedEars' Lahn
    Nov 23, 2005
Loading...

Share This Page