Search code examples
javascripthtmljspsessionprototypejs

Session not expiring after browser window close- Javascript


<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.


Solution

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

    How to end user session when browser closed