[英]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.