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

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

  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
     
    Jiong Feng, Jun 23, 2004
    #3
  4. <script language="Javascript">
    function onUnLoadHandler() {
    alert('Handler runs!');
    }
    </script>
     
    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.
     
    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

    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. 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;
    }
     
    Lasse Reichstein Nielsen, Jun 23, 2004
    #8
  9. Both of these are IE specific. The DOM methods would be:
    Man, you hate M$ the most!
    hehe... :)
     
    CryingClinton, Jun 24, 2004
    #9
  10. 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, Jun 24, 2004
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.