trinStorage.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function TrinStorage() {
  2. }
  3. TrinStorage.prototype.supportsStorage = false;
  4. TrinStorage.prototype.save = function(name, value) {
  5. if (this.supportsStorage) {
  6. try {
  7. localStorage.setItem(name, value);
  8. } catch (e) {
  9. this.supportsStorage = false;
  10. }
  11. }
  12. };
  13. TrinStorage.prototype.load = function(name) {
  14. if (this.supportsStorage) {
  15. try {
  16. return localStorage.getItem(name);
  17. } catch (e) {
  18. this.supportsStorage = false;
  19. }
  20. }
  21. return null;
  22. };
  23. TrinStorage.prototype.remove = function(name) {
  24. if (this.supportsStorage) {
  25. try {
  26. localStorage.removeItem(name);
  27. } catch (e) {
  28. this.supportsStorage = false;
  29. }
  30. }
  31. };
  32. TrinStorage.prototype.clear = function() {
  33. if (this.supportsStorage) {
  34. try {
  35. localStorage.clear();
  36. } catch (e) {
  37. this.supportsStorage = false;
  38. }
  39. }
  40. };
  41. TrinStorage.prototype.length = function() {
  42. if (this.supportsStorage) {
  43. try {
  44. return localStorage.length;
  45. } catch (e) {
  46. this.supportsStorage = false;
  47. }
  48. }
  49. return 0;
  50. };
  51. TrinStorage.prototype.getKey = function(index) {
  52. if (this.supportsStorage && index >= 0 && index < this.length) {
  53. try {
  54. return localStorage.key(key);
  55. } catch (e) {
  56. this.supportsStorage = false;
  57. }
  58. }
  59. return null;
  60. };