storage.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // localStorage
  2. (function() {
  3. function isSupported() {
  4. var item = 'localStoragePollyfill';
  5. try {
  6. localStorage.setItem(item, item);
  7. localStorage.removeItem(item);
  8. sessionStorage.setItem(item, item);
  9. sessionStorage.removeItem(item);
  10. return true;
  11. } catch (e) {
  12. return false;
  13. }
  14. }
  15. if (!isSupported()) {
  16. try {
  17. Storage.prototype._data = {};
  18. Storage.prototype.setItem = function(id, val) {
  19. this._data[id] = String(val);
  20. return this._data[id];
  21. };
  22. Storage.prototype.getItem = function(id) {
  23. return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
  24. };
  25. Storage.prototype.removeItem = function(id) {
  26. return delete this._data[id];
  27. };
  28. Storage.prototype.clear = function() {
  29. this._data = {};
  30. return this._data;
  31. };
  32. } catch (e) {
  33. console.error('localStorage pollyfill error: ', e);
  34. }
  35. }
  36. }());