如何在浏览器上永久保存一些值?

我有一些登录信息,比如用户名,登录电子邮件ID和位置。

我希望即使在用户注销并关闭窗口后,也要在浏览器中保留此信息。

这样,当用户在注销或会话到期后返回时,Web应用程序将填充客户端用户名并向用户请求密码。 我要求的最好例子是谷歌登录。!!

目前我只使用会话而没有cookie。

有哪些可能的解决方案?

我认为您可以使用cookie在客户端存储数据,请点击此链接

http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm

使用方法public void setMaxAge(int expiry)设置存储年龄;

另一个解决方案是HTML5中的本地存储,但仅在最新的浏览器中支持。

http://www.w3schools.com/html/html5_webstorage.asp

http://diveintohtml5.info/storage.html

希望这些链接能为您提供帮助

LocalStorage被认为是在浏览器中永久存储值的最佳解决方案。!! 关于LocalStorage的一个很好的解释可以在这里找到。

这是我用于将值保存到LocalStorage的代码 。

function saveLoginNameToLocalStorage() { if(typeof(Storage)!=="undefined")//checks whether the browser support localStorage { // you dont want to create a variable by var variablename, // just give it as localStorage.yourVariableName, before assigning // any values the variable is shown as undefined. if(localStorage.userName && localStorage.userName !="" && localStorage.userName==document.getElementById("userName").value){ document.getElementById("redirectUrl").value=localStorage.redirectURI; } else{ localStorage.redirectURI=""; document.getElementById("redirectUrl").value=""; } localStorage.userName=document.getElementById("userName").value; localStorage.redirectURI=""; } } 

您可以从浏览器中的任何位置使用localStorage.userName访问变量。 为我工作得很好。 😉

谢谢大家提供的帮助.. !!