Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. function UserDataStorage(maxage) {
  2. // Create a document element and install the special userData
  3. // behavior on it so it gets save() and load() methods.
  4. var memory = document.createElement("div"); // Create an element
  5. memory.style.display = "none"; // Never display it
  6. memory.style.behavior = "url('#default#userData')"; // Attach magic behavior
  7. document.body.appendChild(memory); // Add to the document
  8. // If maxage is specified, expire the userData in maxage seconds
  9. if (maxage) {
  10. var now = new Date().getTime(); // The current time
  11. var expires = now + maxage * 1000; // maxage seconds from now
  12. memory.expires = new Date(expires).toUTCString();
  13. }
  14. // Initialize memory by loading saved values.
  15. // The argument is arbitrary, but must also be passed to save()
  16. memory.load("UserDataStorage"); // Load any stored data
  17. this.getItem = function(key) { // Retrieve saved values from attributes
  18. return memory.getAttribute(key) || null;
  19. };
  20. this.setItem = function(key, value) {
  21. memory.setAttribute(key,value); // Store values as attributes
  22. memory.save("UserDataStorage"); // Save state after any change
  23. };
  24. this.removeItem = function(key) {
  25. memory.removeAttribute(key); // Remove stored value attribute
  26. memory.save("UserDataStorage"); // Save new state
  27. };
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement