Created
December 12, 2012 21:10
-
-
Save mmurph211/4271685 to your computer and use it in GitHub Desktop.
Internet Explorer localStorage polyfill
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
//////////////////////////////////// | |
// | |
// Internet Explorer localStorage polyfill | |
// MIT-style license. Copyright 2012 Matt V. Murphy | |
// | |
//////////////////////////////////// | |
(function(window, document) { | |
"use strict"; | |
var userData, attr, attributes; | |
if (!window.localStorage && (userData = document.body) && userData.addBehavior) { | |
if (userData.addBehavior("#default#userdata")) { | |
userData.load((attr = "localStorage")); | |
attributes = userData.XMLDocument.documentElement.attributes; | |
window.localStorage = { | |
"length" : attributes.length, | |
"key" : function(idx) { return (idx >= this.length) ? null : attributes[idx].name; }, | |
"getItem" : function(key) { return userData.getAttribute(key); }, | |
"setItem" : function(key, value) { | |
userData.setAttribute(key, value); | |
userData.save(attr); | |
this.length += ((userData.getAttribute(key) === null) ? 1 : 0); | |
}, | |
"removeItem" : function(key) { | |
if (userData.getAttribute(key) !== null) { | |
userData.removeAttribute(key); | |
userData.save(attr); | |
this.length = Math.max(0, this.length - 1); | |
} | |
}, | |
"clear" : function() { | |
while (this.length) { userData.removeAttribute(attributes[--this.length].name); } | |
userData.save(attr); | |
} | |
}; | |
} | |
} | |
})(this, this.document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment