Click here to Skip to main content
15,906,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am developing a web application using C# and javaScript. I need to save user date in the browser so that the user once login need not to login on a new tab in same browser. But if he close the browser the data should be cleared.


Can anyone please give me a solution for this?

What I have tried:

I searched online and could not found a solution.

I tried using document.cookie but they are not cleared when browser is closed. The same problem is with localStorage.

I tried sessionStorage but it is cleared when the tab is closed.
Posted
Updated 28-May-18 22:28pm

No. There's no solid way of a server detecting if the client has closed their browser. It's just the nature of web development's asynchronous pattern.

if you really wanted to implement this you can try the following solutions,

How to end user session when browser closed[^]

$(window).on('beforeunload', function(e) {
return "You will now be logged out,";
});

$(window).on('unload', function(e) {
document.cookie = 'hodan_current_cookie=; expires=Fri, 3 Aug 2012 20:47:11 UTC; path=/'
});
 
Share this answer
 
Comments
Jinto Jacob 29-May-18 2:34am    
I tried `unload` method but it will clear data when I reload the window or close the browser tab. I need to keep user data till browser close.
There is currently no solution to this. People are used to either having a universal login that remembers them, or having to log in with each tab, that's just how the internet works.
 
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