Skip to content

Instantly share code, notes, and snippets.

@domleonard
Created September 11, 2021 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domleonard/3b8e1fcbf8592c509ea56553819290d8 to your computer and use it in GitHub Desktop.
Save domleonard/3b8e1fcbf8592c509ea56553819290d8 to your computer and use it in GitHub Desktop.
Local Storage Test using file protocol
<html><head><meta charset="utf-8">
<title>Local Storage Test</title>
<style type="text/css">textarea { width: 80em; height: 20em;}</style>
<script>"use strict";console.log(localStorage);</script>
</head>
<body>
<h1>Local Storage Test</h1>
Open this page and a copy of it as files in a browser and click the buttons to test if pages loaded using the <code>file://</code> protocol are sharing <code>window.localStorage</code>. See the console for an initial log of localStorage.
<p>
<h2>JSON</h2>
<code>Object.entries(localStorage)</code>:
<p>
<textarea id="json"></textarea>
<p>
<button type="button" id=set>Set New Item</button>
<button type="button" id=get>Refresh JSON</button>
<button type="button" id=clr>Clear</button>
<script>"use strict";
get.onclick = function getf() {
const entries = [];
for( var n = 0; n < localStorage.length; ++n) {
let key = localStorage.key(n);
let item = localStorage.getItem(key);
entries.push( [key, item]);
}
json.value = JSON.stringify(entries, null, 2);
}
set.onclick = function setf() {
let string = "Store item number " + (localStorage.length+1);
let key = "key" + localStorage.length;
localStorage.setItem(key, string);
}
clr.onclick = function clearf() {
if(confirm("Do you really want to clear all " + localStorage.length + " items?")) {
localStorage.clear();
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment