简体   繁体   English

如何使用本地存储来存储登录信息?

[英]How can I using local storage to store login information?

My html code like this :我的 html 代码是这样的:

<li id="login" class="hide-on-med-and-down">
    <a class="dark-text" style="padding-right:0 !important" href="#">Login</a>
</li>
<li id="user-login">
    <a class="white-txt dropdown-button" href="#" data-activates="user-dropdown">
        <span id="current-lang" data-code="">085132112345</span>
        <i class="fa fa-angle-down right"></i>
    </a>
    <ul id="user-dropdown" class="dropdown-content">
        <li class=""><a href="#">Profile</a></li>
        <li class="divider"></li>
        <li class="active"><a href="#">Logout</a></li>
        <li class="divider"></li>
    </ul>
</li>

My javascript code like this :我的 javascript 代码是这样的:

var x = document.getElementById("login");
var y = document.getElementById("user-login");
if (localStorage.getItem("loginChelseaLocalStorage") === null) {
    x.style.display = "block";
    y.style.display = "none";
}
else {
    x.style.display = "none";
    y.style.display = "block";
}

If the user login, it will store on local storage like this :如果用户登录,它将存储在本地存储中,如下所示:

localStorage.setItem("loginChelseaLocalStorage", true);

If the user logout, it will remove local storage like this :如果用户注销,它将像这样删除本地存储:

localStorage.removeItem("loginChelseaLocalStorage");

Is the use of local storage to store login information as above is correct and good?如上使用本地存储存储登录信息是否正确且良好?

Localstorage 存储字符串数据,因此您应该使用 JSON.stringify(true) 并从本地存储中获取项目,您可以使用 JSON.parse(getItem("loginChelseaLocalStorage")) 在此处查看 Localstorage 的使用https://www.w3schools。 com/jsref/prop_win_localstorage.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用C#将用户信息存储到本地存储中吗 - Can I store user information to local storage using c# 我如何在PHP旁边使用HTML5本地存储,将其用于php会话,并存储php使用的其他信息 - How can i use HTML5 local storage along side PHP, use that for php sessions, and store other information that php uses laravel每次登录时如何获取本地存储中的数据? - How can I get data in local storage every login on laravel? 我怎样才能将对象存储在本地存储中的角度? - How can i store object in local storage in angular? 如何使用Javascript将cookie存储在本地存储中? - How can I store a cookie in local storage with Javascript? 如何在本地存储中存储 JavaScript 变量? - How can I store a JavaScript variable in local storage? 如何使用jQuery将类存储在本地存储中? - How to store a class in local storage using jquery? 如何使用Protractor存储本地存储数据? - How to store local storage data using Protractor? 我只能将一个学生的详细信息存储到本地存储中。如何在本地存储中使用同一表格存储多个学生的详细信息? - I can only store the one student details to the local storage.How can i store multiple student details using the same form in localstorage? 如何使用PDF JavaScript访问本地浏览器存储? - How can I access local browser storage using PDF JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM