0

<script>
  function ServerPing(){       
  var url = "http://localhost:8080/index.html";
  if (window.XMLHttpRequest){ 
    reqXML = new XMLHttpRequest();           
    reqXML.onreadystatechange = HandlePing;   
    reqXML.open("GET", url, true);         
    reqXML.send(null);                        
  }
  else if(window.ActiveXObject){ 
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
    if(reqXML){ 
      reqXML.onreadystatechange = HandlePing;   
      reqXML.open("GET", url, true);         
      reqXML.send(null);                     
    }
  }
  else{  
     window.location.href = url;
  }       
}
     
function HandlePing(){
  window.status = "Pinging Server - state: " + reqXML.readyState;
  if(reqXML.readyState == 4){
    var strDate = new Date().toString();
    if(reqXML.status == 200){
      window.status = "Sucessfull Ping at " + strDate;
    }
    else{
      window.status = "Failed Ping at " + strDate;
    }
  }
}
 
window.onload = function(){
  var timer = setInterval("ServerPing()",10000)
}
  
  </script>
<HTML>
<HEAD> 
<TITLE> Test</TITLE>
 
  <script>
  function ServerPing(){       
  var url = "http://localhost:8383/fcpbweb/index.html";
  if (window.XMLHttpRequest){ 
    reqXML = new XMLHttpRequest();           
    reqXML.onreadystatechange = HandlePing;   
    reqXML.open("GET", url, true);         
    reqXML.send(null);                        
  }
  else if(window.ActiveXObject){ 
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
    if(reqXML){ 
      reqXML.onreadystatechange = HandlePing;   
      reqXML.open("GET", url, true);         
      reqXML.send(null);                     
    }
  }
  else{  
     window.location.href = url;
  }       
}
     
function HandlePing(){
  window.status = "Pinging Server - state: " + reqXML.readyState;
  if(reqXML.readyState == 4){
    var strDate = new Date().toString();
    if(reqXML.status == 200){
      window.status = "Sucessfull Ping at " + strDate;
    }
    else{
      window.status = "Failed Ping at " + strDate;
    }
  }
}
 
window.onload = function(){
  var timer = setInterval("ServerPing()",10000)
}
  
  </script>
</HEAD>


<BODY>

  <div style="height:300px; width:400px; overflow:hidden; border:1px solid red;">
  
  
  <p style="margin:117px auto 0 auto; width:150px">TEST CONTENT</p>
  
  </div>


</BODY>
</HTML>

When I closing my browser tab my session not getting expire in IE8, chrome & firefox.It works fine in IE9. My main is is JSP.

HTML:

<body onbeforeunload="HandleOnClose()"></body>

JS code:

function HandleOnClose(){

    if (event.clientY < 0) {
                        //event.returnValue = 'Want to leave the page?';
                      location.href=location.protocol+"//"+location.host+"${ctx}/"+logoutLocation;
                    }
                }

I have refered below link also but not worked.

window.onunload is not working properly in Chrome browser. Can any one help me?

I m also using Prototypejs framework. I am open to other solutions also.

Thanks in advance.

Community
  • 1
  • 1
Mike Phils
  • 3,475
  • 5
  • 24
  • 45

2 Answers2

2

You cannot reliably notify the server when the user is closing a tab. There simply isn't an API for that. There are several issues with your example code, not least that trying to force the user to a new location within an onbeforeunload handler is a well-known tactic used by sleezy websites and so good browsers ignore it. And the fact that even a refresh operation (rather than closing the tab) will also fire your handler.

If you need to proactively terminate sessions when users leave your page, the only reliable means of doing so is to have your page routinely ping the server when active, and then have the server terminate the session if it hasn't seen a ping in X minutes. Beware that when a tab is open but in the background, many browsers will limit the extent to which that tab's timers fire, and so you'll need to experiment with your target browsers to find an appropriate interval for your server-side timeout.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Finally, I used AJAX/XMLHttpRequest to do same from below link:

How to end user session when browser closed

Mike Phils
  • 3,475
  • 5
  • 24
  • 45