Created
July 6, 2019 01:07
-
-
Save luisenriquecorona/3c875fc3253dd768006277bf8ac4176b to your computer and use it in GitHub Desktop.
Implements the getItem(), setItem(), and removeItem() methods of the Storage API on top of IE’s userData. (It does not implement key() or clear(), because userData does not define a way to iterate through all stored items.)
This file contains hidden or 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 UserDataStorage(maxage) { | |
// Create a document element and install the special userData | |
// behavior on it so it gets save() and load() methods. | |
var memory = document.createElement("div"); // Create an element | |
memory.style.display = "none"; // Never display it | |
memory.style.behavior = "url('#default#userData')"; // Attach magic behavior | |
document.body.appendChild(memory); // Add to the document | |
// If maxage is specified, expire the userData in maxage seconds | |
if (maxage) { | |
var now = new Date().getTime(); // The current time | |
var expires = now + maxage * 1000; // maxage seconds from now | |
memory.expires = new Date(expires).toUTCString(); | |
} | |
// Initialize memory by loading saved values. | |
// The argument is arbitrary, but must also be passed to save() | |
memory.load("UserDataStorage"); // Load any stored data | |
this.getItem = function(key) { // Retrieve saved values from attributes | |
return memory.getAttribute(key) || null; | |
}; | |
this.setItem = function(key, value) { | |
memory.setAttribute(key,value); // Store values as attributes | |
memory.save("UserDataStorage"); // Save state after any change | |
}; | |
this.removeItem = function(key) { | |
memory.removeAttribute(key); // Remove stored value attribute | |
memory.save("UserDataStorage"); // Save new state | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment