Is there any other option except javascript to fire the Session_End() event when a user directly closes the browser without log off. If I all time check that the client give request to the server or not coz I think when user closes the browser, the server gets no request. So if it is possible how can I implement it? Moreover can I always check if the user is viewing any of my pages and if not can I raise Session_End() event?
-
See http://stackoverflow.com/questions/805463/javascript-to-check-when-the-browser-window-is-close. – John Saunders Dec 17 '13 at 12:39
2 Answers
No, you can't check what the user is doing or when he closes the browser. This would clearly violate against user and data-security rights of the user. Also such a feature would be browser-dependent and therefor hard to implement because every browser has its own interfaces and philosophies.
What you can do is to implement a function in javascript, that sends a signal to your server every minute or so, telling the server that the user is still active.
You can then store the time of the last signal of the user. That way you see that a user was inactive if you haven't received a signal in the last minute or so.

- 4,143
- 22
- 39
-
-
1Check out this tutorial: http://msdn.microsoft.com/en-us/library/bb924552(v=vs.110).aspx – RononDex Dec 17 '13 at 15:17
-
I saw your tutorial and from that I can send request to the server from client. But I want to implement a function in the server that works in a time interval checking any client is connected or not and if the result returns null it calls Session.Abandon(). I am new to asp.net. So can you help me with code and detailed description? – Reshad Dec 19 '13 at 09:17
-
You don't have to do that. The sessions get closed automatically from ASP.NET. Every session has a predefined lifespan and will get abandoned automatically when they reach the end of their lifetime. You can define the lifespan of a session in your web.config – RononDex Dec 19 '13 at 09:34
hii there is one event in javascript to find the browser close event.
but its not work with all browsers.
their is the example.
<HEAD>
<TITLE> TEST</TITLE>
</HEAD>
<script type="text/javascript">
javascript:window.history.forward(1);
var temp = true;
document.onkeydown = keyDownPress;
document.onmousedown = keyDownPress;
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
//alert(is_chrome);
if(is_chrome){
window.onbeforeunload = function(e) {
// confirmExit(e);
alert("1");
alert("2");
alert("3");
return "You are going to close?"; // you can make this dynamic, ofcourse...
}
}else{
window.onbeforeunload = confirmExit;
}
/*if (navigator.userAgent.indexOf('AppleWebKit') > -1) window.onbeforeunload = confirmExit;
else Event.observe(window, 'beforeunload', confirmExit);*/
/*
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(is_chrome){
window.onbeforeunload = function(e) {
confirmExit(e);
return "Are you sure you want to leave this page ?"; // you can make this dynamic, ofcourse...
};}else{
window.onbeforeunload = confirmExit;
}*/
/* if (window.addEventListener) { // all browsers except IE before version 9
window.addEventListener ("beforeunload", OnBeforeUnLoad, false);
}
else {
if (window.attachEvent) { // IE before version 9
window.attachEvent ("onbeforeunload", OnBeforeUnLoad);
}
}
*/
/* function(e){
if(temp==true ){
alert("You are going to close");
// return null;
}
}; */
function keyDownPress(e)
{
var evt = e || window.event;
var keyPressed = evt.which || evt.keyCode;
if (keyPressed==116) {
temp=false;
return false;
}
if(evt.button==2) {
alert("For security reasons, Right click has been disabled!");
return false;
}
}
function confirmExit(e){
// alert("ok ok ");
if(temp==true ){
alert("You are going to close");
}
}
This is code is working fine in IE 8 , Mozilla FF 8 not working on Google Chorme and FF 9

- 178
- 1
- 1
- 11