Skip to content

Instantly share code, notes, and snippets.

@stayradiated
Last active May 5, 2020 15:56
Show Gist options
  • Save stayradiated/1453e075df75ab5f55d0 to your computer and use it in GitHub Desktop.
Save stayradiated/1453e075df75ab5f55d0 to your computer and use it in GitHub Desktop.
Import & Export localStorage between domains
function exportLocalStorage () {
var output = {};
for (var key in localStorage) {
if (!localStorage.hasOwnProperty(key)) {
continue;
}
output[key] = localStorage[key];
}
return JSON.stringify(output).replace(/'/g, '\\\'').replace(/"/g, '\\"');
}
function importLocalStorage (input) {
input = JSON.parse(input);
localStorage.clear();
for (var key in input) {
if (!input.hasOwnProperty(key)) {
continue;
}
localStorage.setItem(key, input[key]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment