0

In my chat application,I need an confirmation box to logout when closing the window.

Ok button in confirmation box works fine.but,

If i press, cancel in the confirmation box,I dont need to close the browser window..

In my case if i press cancel ,My browser window was closed...Please help me...

window.onunload = function () {

    var confirmation = confirm("Are you Sur want to logout the session ?");
    if (confirmation == true)
      {
        if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
            logout();
     // confirmation = "You pressed OK!";
      }
    else
      {
     // confirmation = "You pressed Cancel!";


      }
};

In logout code,

function logout(){
    //alert("<---------->"+userId+";"+secureKey+";"+name);
    clearInterval(timer);
    document.getElementById("button3").style.display = "none";
    document.getElementById("message").innerText = "";
    try
    {
        xmlhttp.onreadystatechange=function()
        {
            //alert("Status : "+xmlhttp.status+"\nreadyState : "+xmlhttp.readyState);
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                //alert("<---------->"+userId+";"+secureKey+";"+name);
                //alert(xmlhttp.responseText.toString());
            }
        };
        xmlhttp.open("POST","LogoutAction?&userId="+userId+"&secureKey="+secureKey+"&nickname="+name,true);
        xmlhttp.send();
    }
    catch(err)
    {
        alert(err.description);
    }
}

In LogoutAction Servlet,

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        secureKey = request.getParameter("secureKey");
        userId = request.getParameter("userId");
        //nickname = request.getParameter(nickname);
        protocol = ApplicationInfo.flexProtocol;

        logout = new Logout();
        logout.requestLogout(secureKey, userId, null, protocol);
        //out.println(secureKey+";"+userId+";"+nickname);
    }

In java code,

public class Logout {
public void requestLogout(String secureKey, String userId, String nickname, FlexChatProtocol protocol) {

        RequestLogout logout = null;

        Message resp  = null;
        logout = RequestLogout.create();
        logout.setSecureKey(secureKey);
        logout.setUserId(userId);
            try {

            resp = protocol.request(logout);
            System.out.println(resp);


        } catch (ProtocolException e) {

        } catch (IllegalStateException e) {

        } 
    }
}

Thanks in advance..

4

2 回答 2

1

you should use onbeforeunload.. like this:

window.onbeforeunload = function () {

var confirmation = confirm("Are you Sur want to logout the session ?");
if (confirmation == true)
  {
    if((sessionId != null)&&(sessionId!="null")&& (sessionId != ""))
        logout();
 // confirmation = "You pressed OK!";
  }
else
  {
 // confirmation = "You pressed Cancel!";


  }

};

于 2012-11-22T21:25:38.437 回答
0

You can't prevent a user closing a window. This is actually a feature of browsers to prevent malicious sites keeping a window open. Think of what would happen with pop-up advertisements if you couldn't close the window.

Unload handlers can prevent the user navigating away, but not closing the window.

于 2012-11-16T09:27:54.967 回答