Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,everyBody

I want to run a server side function when user close the browser. (not refresh it)
I try unload and beforUnload events But these events also fire when the page refreshed.
So I can not use these events, I want run the function JUST when user CLOSE the browser.
Or how can I predict is browser close or refresh fired
In fact I want to write a code that when a user log in to my website another user with same username can not log in until the first user log out.
So when first user log in I check my data base and after he/she log in I update the a table So nobody can log in until the user log out that I update the table again.

Also I can not use cookies because user can delete them!

anybody can help me?
Posted
Updated 25-Aug-11 5:00am
v4

you can use the onbeforeunload event to run a javascript which tells your server that the user closed the website.
HTML
<body onbeforeunload="alert('User is closing my website :'(');">


To communicate with your server via javascript see Call Server Side Code by JavaScript using Ajax.NET Framework[^]
 
Share this answer
 
v2
Comments
TheyCallMeMrJames 25-Aug-11 9:13am    
Isn't that going to kill them when they click the back button? A link?
Simon Bang Terkildsen 25-Aug-11 9:16am    
kill them in what sense?
koolprasad2003 25-Aug-11 9:23am    
onbeforeunload get called even when u click "refresh".
so u can't directly predict is browser closed or refesh fired.
Simon Bang Terkildsen 25-Aug-11 9:24am    
very true, didn't consider refresh
koolprasad2003 25-Aug-11 10:07am    
check my reply. this event will fire only when browser closed
I don't believe that I've ever seen a cross-browser version of something that works.

The best bet is to create a login cookie that expires when the browser closes. When the user hits this site - even if they are 'logged in' - and the cookie doesn't exist, you can force the logout.

Also, make it painfully obvious to users that they should log out, and if it's that important, make it easy to find.

Cheers.
 
Share this answer
 
Comments
Ankur\m/ 25-Aug-11 9:18am    
Yes, the best option to handle this scenario that I can think of. 5!
you have to take help of javascript, call following function on
onbeforeunload()

C#
function handleWindowClose()
         {
            if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
                event.returnValue = "Browser Closed";
            }
        }

then after call AJAX to execute code behind method.
 
Share this answer
 
v2
I might have it even way easier than you

JavaScript
var isClose = false;
//this code will handle the F5 or Ctrl+F5 key+
//need to handle more cases like ctrl+R whose codes are not listed here
document.onkeydown = checkKeycode
window.onbeforeunload = doUnload;
function checkKeycode(e)
{
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    if(keycode == 116)
    {
        isClose = true;
    }
}

function somefunction()
{
    isClose = true;
}

function doUnload()
{
    if(!isClose)
    {
        window.location = "LogOut.aspx"
    }
}


Just add on the body tag
JavaScript
onmousedown="somefunction()"


This will launch the "LogOut.aspx" page whenever the window or the tab is closed, then you can have there that server side code you need. If you need to pass some data to the page, use the Session variable to handle the data.

Now, if you have many pages, and you need to launch that function from either page is opened, you i'll need to post this code in each page, reason why i suggest using a master page, and only put this code there
 
Share this answer
 
Comments
Maidam santhosh 22-Jul-13 6:56am    
This is Working In only FireFox... pls suggest me.. i want work in all browsers.
Perhaps you could write a solution in Node.js? Have a node.js server that listens for all incoming "logins". Then on your site, you have a client-listener that accepts requests with a given userID. IF that user-id is logging in from another source, you can have the node.js client log the user out from the first log in souce.
 
Share this answer
 

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