我目前正在我的本地机器上进行测试,所以当用户按下 Sign out
// The ********** is my site name, which i don't want releasing yet
http://localhost:8080/**********/logout/
本地存储项:
Key: tkn Value: EE8D9DA0F1D71A0BE8B80F3BA80343555655871151tk16
我尝试过以下测试:
if (localStorage.getItem('tkn')) {
localStorage.clear();
}
if (localStorage.getItem('tkn')) {
localStorage.removeItem('tkn');
}
// I've also ran these both tests in the console of the browser
// and they return back 'undefined' but the item is cleared.
但是,当我注销
时,用户 session 已完全删除。但是 localStorage 似乎没有被清除。
有人可以帮我解释一下吗?
最佳答案
不是检查特定属性,而是检查长度
if (localStorage.length > 0 ) {
localStorage.clear();
}
localStorage 是 Storage 的一个实现接口(interface)和存储有一个 property称为长度
Storage.length
Returns an integer representing the number of data items stored in the Storage object.
关于javascript - 如何清除localStorage? (JavaScript/jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534081/