有没有办法用javascript重置/清除浏览器的localStorage?
当前回答
如果要从用户的本地存储中删除特定项或变量,可以使用
localStorage.removeItem("name of localStorage variable you want to remove");
其他回答
使用此选项清除localStorage:
localStorage.clear();
清除sessionStorage
sessionStorage.clear();
如果要从用户的本地存储中删除特定项或变量,可以使用
localStorage.removeItem("name of localStorage variable you want to remove");
本地存储附加在全局窗口上。当我们在chrome devtools中记录本地存储时,我们看到它具有以下API:
我们可以使用以下API删除项目:
localStorage.clear():清除整个localStoragelocalStorage.removeItem('myItem'):删除单个项目
下面是一个简单的代码,它将使用javascript清除存储在浏览器中的本地存储
<script type="text/javascript">
if(localStorage) { // Check if the localStorage object exists
localStorage.clear() //clears the localstorage
} else {
alert("Sorry, no local storage."); //an alert if localstorage is non-existing
}
</script>
要确认本地存储是否为空,请使用以下代码:
<script type="text/javascript">
// Check if the localStorage object exists
if(localStorage) {
alert("Am still here, " + localStorage.getItem("your object name")); //put the object name
} else {
alert("Sorry, i've been deleted ."); //an alert
}
</script>
如果返回null,则清除本地存储。