Skip to content

Instantly share code, notes, and snippets.

@kejun
Created September 5, 2013 10:09
Show Gist options
  • Save kejun/6448301 to your computer and use it in GitHub Desktop.
Save kejun/6448301 to your computer and use it in GitHub Desktop.
Untitled
<!-- content to be placed inside <body>…</body> -->
;(function() {
this.localStorage = window.localStorage || function() {
var doc = document;
if (doc.documentElement.addBehavior) {
throw 'don\'t support localstorage or userdata';
return;
}
var storeName = '_localstorage';
var node = doc.createElement('input');
node.type = 'hidden';
var _wrap = function(fn) {
return function(){
doc.body.appendChild(node);
node.addBehavior('#default#userData');
var expires = new Date();
expires.setDate(expires.getDate() + 365);
node.expires = expires.toUTCString();
node.load(storeName);
var result = fn.apply(node, arguments);
doc.body.removeChild(node);
return result;
};
};
return {
getItem: _wrap(function(key) {
return this.getAttribute(key);
}),
setItem: _wrap(function(key, val) {
this.setAttribute(key, val);
this.save(storeName);
}),
removeItem: _wrap(function(key) {
this.removeAttribute(key);
this.save(storeName);
}),
clear: _wrap(function() {
var attrs = this.XMLDocument.documentElement.attributes;
for (var i=0, attr; attr=attrs[i]; i++) {
this.removeAttribute(attr.name)
}
this.save(storeName);
})
}
}();
})();
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"javascript"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment