localStorage polyfill with check first
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
(function(){ | |
if( | |
!(function(t){ | |
try { | |
window.localStorage.setItem(t,t); | |
window.localStorage.removeItem(t); | |
return true; | |
} catch(e) { | |
return false; | |
} | |
})('t')) { | |
window.localStorage = { | |
getItem: function (sKey) { | |
if (!sKey || !this.hasOwnProperty(sKey)) { return null; } | |
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); | |
}, | |
key: function (nKeyId) { | |
return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]); | |
}, | |
setItem: function (sKey, sValue) { | |
if(!sKey) { return; } | |
document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; | |
this.length = document.cookie.match(/\=/g).length; | |
}, | |
length: 0, | |
removeItem: function (sKey) { | |
if (!sKey || !this.hasOwnProperty(sKey)) { return; } | |
document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; | |
this.length--; | |
}, | |
hasOwnProperty: function (sKey) { | |
return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); | |
} | |
}; | |
window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length; | |
} | |
})(); |
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
(function(){try{window.localStorage.setItem("t","t");window.localStorage.removeItem("t");var b=!0}catch(a){b=!1}b||(window.localStorage={getItem:function(a){return a&&this.hasOwnProperty(a)?unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)"+escape(a).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"),"$1")):null},key:function(a){return unescape(document.cookie.replace(/\s*=(?:.(?!;))*$/,"").split(/\s*=(?:[^;](?!;))*[^;]?;\s*/)[a])},setItem:function(a,b){a&&(document.cookie= | |
escape(a)+"="+escape(b)+"; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/",this.length=document.cookie.match(/=/g).length)},length:0,removeItem:function(a){a&&this.hasOwnProperty(a)&&(document.cookie=escape(a)+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/",this.length--)},hasOwnProperty:function(a){return(new RegExp("(?:^|;\\s*)"+escape(a).replace(/[\-\.\+\*]/g,"\\$&")+"\\s*\\=")).test(document.cookie)}},window.localStorage.length=(document.cookie.match(/=/g)||window.localStorage).length)})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment