如何在浏览器中永久保存一些值? [英] How to save some values permanently on a browser?

查看:206
本文介绍了如何在浏览器中永久保存一些值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some login information, let say user name, login email Id and location.

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

I want keep this information in the browser even after the user logout and close the window.

通过这种方式,当用户在注销或会话期满后再次出现时,Web应用程序将填写客户端用户名,并要求用户输入密码. 我要求的最好的例子是google登录.!

By this when user comes back after a logout or session expiry, the web application fills the client user name and ask for the password from the user. The best example of my requirement is google login.!!

目前,我仅使用会话,不使用Cookie.

Currently i am using only session and no cookies.

可能的解决方案是什么?

What are the possible solutions.?

推荐答案

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

LocalStorage is considered to be the best solution for storing values permanently in the browser.!! A good explanation about the LocalStorage can be found here.

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

This is my code used to save the value to the 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访问变量.为我工作得很好. ;-)

You can access the variable using localStorage.userName from anywhere in the browser. Worked well for me. ;-)

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

Thanks everyone for the help provided..!!

这篇关于如何在浏览器中永久保存一些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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