有没有办法用javascript重置/清除浏览器的localStorage?


当前回答

window.localStorage.clear(); //try this to clear all local storage

其他回答

手动按钮:

<script>
function ask() {
if (confirm('Clear localStorage?') == true) {
localStorage.clear()
location.reload()
}
else {
alert('Nothing happend')
}
}
}
</script>
<style>
button {border-width:0px;background-color:#efefef;padding:5px;width:5cm;margin:5px;}
</style>
<button onclick=ask()>Clear localStorage</button>

清除sessionStorage

sessionStorage.clear();

使用此选项清除localStorage:

localStorage.clear();
window.localStorage.clear(); //try this to clear all local storage

下面是一个简单的代码,它将使用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,则清除本地存储。