Last active
August 29, 2015 14:22
-
-
Save Pradeek/67f45f7bfeb24419b9c0 to your computer and use it in GitHub Desktop.
Local storage shim that works across old IEs and Safari's Private Browsing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Local storage shim | |
* Taken from https://gist.github.com/remy/350433 | |
* Local storage detection taken from https://gist.github.com/paulirish/5558557 | |
* Modified to add {encode/decode}URIComponent to fix Safari's cookie semi-colon freak out. | |
***/ | |
function doesLocalStorageEvenWork() { | |
try { | |
var str = "localStorage"; | |
window.localStorage.setItem(str, str); | |
window.localStorage.removeItem(str); | |
return true; | |
} catch(e) { | |
return false; | |
} | |
} | |
if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined' || !doesLocalStorageEvenWork() ) (function () { | |
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) { | |
if(!data) { return null; } | |
return data[key] === undefined ? null : decodeURIComponent(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) { | |
delete data[key]; | |
this.length--; | |
setData(data); | |
}, | |
setItem: function (key, value) { | |
data[key] = encodeURIComponent(value+''); // forces the value to a string | |
this.length++; | |
setData(data); | |
} | |
}; | |
}; | |
if (typeof window.localStorage == 'undefined' || !doesLocalStorageEvenWork() ) { | |
window.localStore = new Storage('local'); | |
} | |
if (typeof window.sessionStorage == 'undefined') window.sessionStorage = new Storage('session'); | |
if(!window.hfcStore && window.localStorage) { | |
window.localStore = window.localStorage; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment