有没有办法用javascript重置/清除浏览器的localStorage?
当前回答
如果要从用户的本地存储中删除特定项或变量,可以使用
localStorage.removeItem("name of localStorage variable you want to remove");
其他回答
下面是一个简单的代码,它将使用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,则清除本地存储。
使用此选项清除localStorage:
localStorage.clear();
如果要清除存储在localStorage中的所有项目,则
localStorage.clear();
使用此选项清除所有存储的密钥。
如果您只想清除/删除特定的键/值,则可以使用removeItem(键)。
localStorage.removeItem('yourKey');
window.localStorage.clear(); //try this to clear all local storage
清除sessionStorage
sessionStorage.clear();
推荐文章
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie
- 如何减去日期/时间在JavaScript?
- 如何检测“搜索”HTML5输入的清除?
- 字符串中的单词大写