1

I have the following html code to clear web storage data after click the button "Clear storage".

The explorer (chrome and firefox) just never trigger the function clear after click the button of clear storage.

The code is:

<html>
<head>
<script>

function clear() {
    console.log();
    localStorage.clear();
    refreshContents();
    alert("all cache data cleared!");
}

function refreshContents() {
var str = "";

alert("in refresh");
for (var i = 0, len = localStorage.length; i < len; i++) {
    var k = localStorage.key(i);
    var v = localStorage.getItem(k);
   str += "'" + k + "' = '" + v + "'<br />";
 }
alert("after for loop");
key.value = "";
value.value = "";
content.innerHTML = str;
}
</script>
</head>
<body>
<div class="content"> <!-- end .content -->
<p>This is the logout page.</p>
<p>All temporary data will be erased after logout.    
<p>&nbsp;</p>
<input type="button" onclick="clear();" value="Clear Storage" />&nbsp;
Contents of Local Storage:<br />
<span id="content"></span>
</body>
</html>

Can someone help please. Thanks in advance!

2 Answers2

0

Changing the function name did the trick. Just change your function name from clear to anything say clearABC and it will work.

function clearABC()
{
}

The reason for this in this post: is clear a reserved word in javascript

Hope this helps, Cheers

Happy coding !!

Community
  • 1
  • 1
Arti
  • 2,993
  • 11
  • 68
  • 121
-2
<script type="text/javascript"> 
function clear() {
    console.log();
    localStorage.clear();
    refreshContents();
    alert("all cache data cleared!");
}

function refreshContents() {
var str = "";

alert("in refresh");
for (var i = 0, len = localStorage.length; i < len; i++) {
    var k = localStorage.key(i);
    var v = localStorage.getItem(k);
   str += "'" + k + "' = '" + v + "'<br />";
 }
alert("after for loop");
key.value = "";
value.value = "";
content.innerHTML = str;
}
</script>

Update Script tag

I think this one help you

  • No. JavaScript is the default type and, as of HTML 5, the type attribute is optional. The only thing that it does, when you are writing JavaScript, is give you the opportunity to make a typo and break the script. – Quentin Sep 27 '16 at 07:54