Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have implemented windows.onbeforeunload as.
// Body tag is as
<body  önmousedown="BodyClicked()">

window.onbeforeunload = WindowCloseHanlder;
    var isClose = false;
    function WindowCloseHanlder() {
//        alert(document.getElementById('a_TestMe').title);
        if (!isClose) {
                 $.ajax({
                    type: "GET",
                    url: "/Account/LogOff" // the URL of the controller action method
                });
        }
    }
    function BodyClicked() {
        isClose = true;
    }

The method LogOff is called only if i close tab but not on direct browser close.

How could i achieve this ?

Please guide me for this...
Posted
Updated 9-Jul-17 23:06pm
Comments
F-ES Sitecore 10-Jul-17 5:35am    
Probably because the browser process is gone so can't run your code. These kinds of things are useless anyway, for the last 15 years people have been asking how to know when the user has closed their browser and after 15 years of asking it still can't be done. What if I don't close my browser but instead leave it idle for a year, don't you want to log me out then?

The web is stateless, the server doesn't know if the client is there or not and all web technology revolves around this concept. Any attempt to do otherwise is doomed to fail.
Richard Deeming 10-Jul-17 13:35pm    
2012! :)

I guess the commenter on Solution 1 initially posted their comment as a solution, which dragged this question back up into the "active" list.
F-ES Sitecore 11-Jul-17 4:27am    
The irony :D

1 solution

You need to return false if the ajax call is not successful. True when it is successful. Also it would probably be good to have the ajax call not be async. See the example below. This example may not fix your problem but it should help you to track it down better if it doesn't fix it.

I have had problems with the onbeforeunload event not always firing. If I remember correctly it doesn't fire if the user closes the browser before the page has fully loaded. I don't believe there is anything that you can do to fix that problem.

JavaScript
function WindowCloseHanlder() {
     var success = false;
     if (!isClose) {     
          $.ajax({
               type: "GET",
               url: "/Account/LogOff",
               async: false,
               error: function (xhr, ajaxOptions, thrownError) {
                    success = false;
               }
               success: function (data) {
                    success = true;
               }
          )};
      }
      return success;
}  
 
Share this answer
 
Comments
Member 13301772 10-Jul-17 5:09am    
But above solution is not working for me for browser close. Please assist.
GateKeeper22 10-Jul-17 7:42am    
The solution won't always work. If you read the second paragraph of my answer I explain why. Sorry that I can't be any more help then that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900