1

我以前的帖子是window.onbeforeunload 和 window.onunload 在 Firefox、Safari、Opera 中不起作用?在这种情况下它不起作用。所以我创建了一个新线程。

现在这适用于IEsafarichrome

唯一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 !");   %>
4

1 回答 1

-1

你需要从你的卸载事件回调中返回一些东西 -

$(window).unload(function() {
        closeWindow();
        return "some string";
});
于 2013-04-23T09:30:57.943 回答