我之前的帖子将是window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?,并且在这种情况下无效。所以我创建了一个新帖子。
现在,这适用于IE
,safari
和chrome
。
仅在beforeunload
和firefox
中触发的unload
事件未被解雇。
两者都不在Opera
。
我的jquery将是,
$(window).on('beforeunload', function() {
return 'Are you sure want to LOGOUT the session ?';
});
$(window).unload(function() {
closeWindow();
});
function closeWindow(){
alert("fdf");
$.ajax(
{
type: "post",
url: "Test.jsp",
async: false,
//data : "userId="+encodeURIComponent(userId)+"&secureKey="+encodeURIComponent(secureKey)+"&nickname="+encodeURIComponent(name)+"&sid="+Math.random() ,
cache:false,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
scriptCharset: "utf-8" ,
dataType: "html",
//alert(sessionId+" "+userId+ " "+secureKey+" "+message);
error: function (xhr, ajaxOptions, thrownError) {
//alert("status :"+xhr.status+"\nthrownError : "+thrownError+"\nresponseText : "+xhr.responseText);
},
success: function( responseData , status){
//alert("LogoutAction Response Data : "+ responseData +"\nStatus : "+status);
var checkMsg = $.trim(responseData);
if(checkMsg != "null" && checkMsg.length != 0 && checkMsg != null){
alert("Window close : "+checkMsg);
}
}
}
);
}
我的Test.jsp
将是,
<% System.out.println("Window is closed !");
out.println("Window is closed !"); %>
答案 0 :(得分:-1)
您需要从卸载事件回调中返回一些内容 -
$(window).unload(function() {
closeWindow();
return "some string";
});