Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A LocalStorage polyfill that also works in private browsing Safari (by doing window.localStorage.__proto__ = ourPolyfillObject) (where window.localStorage !== 'undefined', but has a quota of 0 and throws an error when you try to setItem)
//variant of https://gist.github.com/Contra/6368485
(function () {
var isStorageAvailable = function (storage) {
if (typeof storage == 'undefined') return false;
try { // hack for safari incognito
storage.setItem("storage", "");
storage.getItem("storage");
storage.removeItem("storage");
return true;
}
catch (err) {
return false;
}
};
var localStorageAvailable = isStorageAvailable(window.localStorage),
sessionStorageAvailable = isStorageAvailable(window.sessionStorage);
if (!localStorageAvailable || !sessionStorageAvailable) {
var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i, c;
for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
function setData(data) {
data = JSON.stringify(data);
if (type == 'session') {
window.name = data;
} else {
createCookie('localStorage', data, 365);
}
}
function clearData() {
if (type == 'session') {
window.name = '';
} else {
createCookie('localStorage', '', 365);
}
}
function getData() {
var data = type == 'session' ? window.name : readCookie('localStorage');
return data ? JSON.parse(data) : {};
}
// initialise if there's already data
var data = getData();
return {
length: 0,
clear: function () {
data = {};
this.length = 0;
clearData();
},
getItem: function (key) {
return data[key] === undefined ? null : data[key];
},
key: function (i) {
// not perfect, but works
var ctr = 0;
for (var k in data) {
if (ctr == i) return k;
else ctr++;
}
return null;
},
removeItem: function (key) {
if (data[key] === undefined) this.length--;
delete data[key];
setData(data);
},
setItem: function (key, value) {
if (data[key] === undefined) this.length++;
data[key] = value+''; // forces the value to a string
setData(data);
}
};
};
if (!localStorageAvailable) window.localStorage.__proto__ = new Storage('local');
if (!sessionStorageAvailable) window.sessionStorage.__proto__ = new Storage('session');
}
})();
@pgilad

This comment has been minimized.

Copy link

pgilad commented Mar 10, 2014

Doesn't work if Cookies/data is disabled in Chrome.
You cannot access window.localStorage without getting an error thrown (which means you have to wrap check in try/catch and also cannot set proto)

@ibnIrshad

This comment has been minimized.

Copy link

ibnIrshad commented Sep 16, 2015

This doesn't work on Safari 9.0 on OS X El Capitan. I get: Error: Attempted to assign to readonly property.

@wembernard

This comment has been minimized.

Copy link

wembernard commented May 20, 2016

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.