localstorage.js 506 B

123456789101112131415161718192021222324252627282930313233
  1. window.ls = {};
  2. window.ls.get = function(key) {
  3. return JSON.parse(localStorage.getItem(key));
  4. }
  5. window.ls.set = function(key, val) {
  6. try {
  7. localStorage.setItem(key, JSON.stringify(val));
  8. return true;
  9. } catch(e) {
  10. return false;
  11. }
  12. }
  13. window.ls.del = function(key) {
  14. try {
  15. localStorage.removeItem(key);
  16. return true;
  17. } catch(e) {
  18. return false;
  19. }
  20. }
  21. window.ls.clear = function() {
  22. try {
  23. localStorage.clear();
  24. return true;
  25. } catch(e) {
  26. return false;
  27. }
  28. }