在我的聊天应用程序中,关闭窗口时需要一个确认框来注销。
确认框中的确定按钮工作正常。但是,
如果我按,在确认框中取消,我不需要关闭浏览器窗口..
在我的情况下,如果我按下取消,我的浏览器窗口已关闭...请帮助我......
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!";
}
};
在退出代码中,
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);
}
}
在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);
}
在java代码中,
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) {
}
}
}
提前致谢..
答案 0 :(得分:1)
你应该使用onbeforeunload ..像这样:
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!";
}
};
答案 1 :(得分:0)
您无法阻止用户关闭窗口。这实际上是浏览器的一项功能,可以防止恶意网站保持窗口打开状态。想想如果你无法关闭窗口,弹出式广告会发生什么。
卸载处理程序可以阻止用户导航,但不会关闭窗口。