如何在浏览器关闭之前保留用户名? [英] How do I keep user name till browser close?

查看:231
本文介绍了如何在浏览器关闭之前保留用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# javaScript 开发一个Web应用程序。我需要在浏览器中保存用户日期,以便用户登录后无需登录同一浏览器中的新选项卡。但如果他关闭浏览器,数据应该被清除。





有谁能请给我一个解决方案?



我尝试了什么:



我在网上搜索但找不到解决方案。



我尝试使用 document.cookie 但是当浏览器关闭时它们不会被清除。同样的问题是 localStorage



我试过 sessionStorage 但是当标签关闭时它会被清除。

解决方案

没有。服务器没有可靠的方法来检测客户端是否关闭了浏览器。这只是Web开发异步模式的本质。



如果你真的想实现这个,你可以尝试以下解决方案,



如何在浏览器关闭时结束用户会话 [ ^ ]



 


(窗口).on('beforeunload' ,function(e){
return你现在将被注销;
});


(window).on('unload',function (e){
document.cookie ='hodan_current_cookie =; expires = Fri,2012年8月3日20:47:11 UTC; path = /'
});


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.

解决方案

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=/' });


这篇关于如何在浏览器关闭之前保留用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆