location.replace() in the onUnload event?

Discussion in 'Javascript' started by Andoni, Sep 26, 2003.

  1. Andoni

    Andoni Guest

    Hi,

    I am only writing for IE 5.5+ so no need for compatibility at all ;-)

    I am trying to get my users to logoff which they finish on a particular
    page. This is no problem (or should be no problem) as I can just put a
    location.replace('http://www.mysite.com/html/logoff.jsp'); into the onUnload
    event of the page they always finish on. This should fire when they click
    the X in the corner (my app. is in a popup window) instead of hitting exit.

    Unfortunately this won't work for me as it won't go to the page from the
    onUnload= event unless I put an alert box in the method called like this:
    (Example 1 does not work):

    function endSession()
    {
    if(!submitting)
    {
    // end session.
    location.replace('http://www.mysite.com/jsp/logoff.jsp');
    }
    return false;
    }


    function endSession()
    {
    if(!submitting)
    {
    // end session.
    location.replace('http://www.mysite.com/jsp/logoff.jsp');
    alert('in endSession()');
    }
    return false;
    }

    Can anyone shed any light on why the second one works? What can I use
    instead of an alert box?

    Thanks in advance,

    Andoni.
     
    Andoni, Sep 26, 2003
    #1
    1. Advertisements

  2. You can open a logoff window with
    <body onunload="window.open('logon.jsp');">
    then logon.jsp can call
    <body onload="window.close();">
    if you so desire.
     
    Martin Honnen, Sep 26, 2003
    #2
    1. Advertisements

  3. Andoni

    Andoni Guest

    Thanks for this, I was just reading the M$ site when I noticed that my
    location.replace was itself also calling the onUnload event! You are right,
    this way works well, thanks.

    Andoni.
     
    Andoni, Sep 26, 2003
    #3
  4. If you are writing exclusively for IE 5.5+ you might be better off using
    the onbeforeunload event.

    Richard.
     
    Richard Cornford, Sep 26, 2003
    #4
    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.