1

My previous post is window.onbeforeunload and window.onunload is not working in Firefox, Safari, Opera? and it not worked in this situation. So I created a new thread.

Now this works in IE, safari, and chrome.

only beforeunload event fired in firefox and unload not fired.

Both not working in Opera.

My jQuery will be:

$(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);
      }
    },
  });
}

My Test.jsp will be ,

<%  System.out.println("Window is closed !"); 
out.println("Window is closed !");   %>
xxx
  • 1,153
  • 1
  • 11
  • 23
Human Being
  • 8,269
  • 28
  • 93
  • 136

1 Answers1

-1

You need to return something from your unload event callback -

$(window).unload(function() {
        closeWindow();
        return "some string";
});
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111