javascript缓存类 Storage.js

      javascript 2009-3-2 3:37

这个是可以将用户数据缓存到本地浏览器.据说可以缓存100k以上数据,支持ie,firefox2以上,不占用cookie.当cookie 数据太大时这个可以是首选.
以下是为 ecmall 写的一个缓存类.所有方法使用动态绑定.在确认浏览器后才把对应的方法绑定上去.该类使用了ecmall自己的框架,我把涉及的部分都放到Storage类后面.该类还缺一个json解析类,可以自己去找.

Storage.js类

  1. var Storage = new Object();
  2. Object.extend(Storage, (function(){
  3.   var _storage = false;
  4.   if (navigator.isIE()) document.documentElement.addBehavior("#default#userdata");
  5.   if (typeof(sessionStorage) != 'undefined') _storage = sessionStorage;
  6.  
  7.   if (navigator.isIE()){
  8.     var _add = function(key, value){
  9.       with(document.documentElement) 
  10.       try {
  11.         load(key);
  12.         setAttribute("value", value);
  13.         save(key);
  14.         return  getAttribute("value");
  15.       } catch (ex){
  16.         return null;
  17.       }
  18.     };
  19.    
  20.     var _get = function(key) {
  21.       with(document.documentElement)
  22.       try {
  23.         load(key);
  24.         return  getAttribute("value");
  25.       } catch (ex){
  26.         return null;
  27.       }
  28.     };
  29.    
  30.     var _remove = function(key) {
  31.       with(document.documentElement)
  32.       try {
  33.         load(key);
  34.         removeAttribute("value")
  35.         save(key);
  36.       } catch(ex){}
  37.     };
  38.    
  39.   } else if(navigator.isFirefox() && _storage){
  40.     var _add = function(key, value){
  41.       sessionStorage.setItem(key,value);
  42.     };
  43.  
  44.     var _get = function(key) {
  45.       return sessionStorage.getItem(key)
  46.     };
  47.    
  48.     var _remove = function(key) {
  49.       var value = undefined;
  50.       sessionStorage.setItem(key,value);
  51.     };
  52.   } else {
  53.     var _add = function(key, value){
  54.       document.setCookie(key, value);
  55.     };
  56.  
  57.     var _get = function(key) {
  58.       return document.getCookie(key);
  59.     };
  60.    
  61.     var _remove = function(key) {
  62.       document.removeCookie(key);
  63.     };
  64.   }
  65.  
  66.   return {
  67.     add: function(key, value) {
  68.       if (typeof(value) != 'string') {
  69.         value = value.toJSONString();
  70.       }
  71.       _add(key, value);
  72.     },
  73.     get: function(key){
  74.       return _get(key);
  75.     },
  76.     remove: function(key){
  77.       _remove(key);
  78.     }
  79.   };
  80. })())

ecmall 框架截取

  1. Object.extend = function(destination, source){
  2.   for (property in source)
  3.     destination[property] = source[property];
  4.   return destination;
  5. };
  6.  
  7.  
  8. /* Navigator Extensions */
  9. Object.extend(navigator, {
  10.   isIE : function() { return this.userAgent.toLowerCase().indexOf("msie") != - 1; },
  11.   isFirefox : function() { return this.userAgent.toLowerCase().indexOf("firefox") != - 1; },
  12.   isSafari : function() { return this.userAgent.toLowerCase().indexOf("safari") != - 1; },
  13.   isOpera : function() { return this.userAgent.toLowerCase().indexOf("opera") != - 1; }
  14. });
  15.  
  16.  
  17. /* Document Extensions */
  18. Object.extend(document, {
  19.   getCookie : function(sName) {
  20.     var aCookie = this.cookie.split("; ");
  21.     for (var i=0; i < aCookie.length; i++){
  22.       var aCrumb = aCookie[i].split("=");
  23.       if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
  24.     }
  25.     return null;
  26.   },
  27.  
  28.   setCookie : function(sName, sValue, sExpires) {
  29.     var sCookie = sName + "=" + encodeURIComponent(sValue);
  30.     if (sExpires != null) sCookie += "; expires=" + sExpires;
  31.     this.cookie = sCookie;
  32.   },
  33.  
  34.   removeCookie : function(sName) {
  35.     this.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
  36.   },
  37.  
  38.   require : function(path, callback, type){
  39.     var s,i;
  40.     var id = path.replace(".","").replace("/","");
  41.  
  42.     if (!type || type == "js"){
  43.       var ss = this.getElementsByTagName("script");
  44.       for(i =0;i < ss.length; i++){
  45.         if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
  46.       }
  47.  
  48.       s      = $ ce("script");
  49.       s.id   = id;
  50.       s.type = "text/javascript";
  51.       s.src  = path;
  52.     }
  53.     else {
  54.       var ss = this.getElementsByTagName("link");
  55.       for(i =0;i < ss.length; i++){
  56.         if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
  57.       }
  58.  
  59.       s = $ ce("link");
  60.       s = document.createElement("link");
  61.       s.rel = "stylesheet";
  62.       s.type = "text/css";
  63.       s.href = path;
  64.       s.disabled = false;
  65.     }
  66.     var head = this.getElementsByTagName("head")[0];
  67.     head.appendChild(s);
  68.     if (callback) {
  69.       if (!navigator.isIE() && type == "css") {
  70.         window.setTimeout(function(){callback.call()}, 100);
  71.       }
  72.       else {
  73.         s.onload = s.onreadystatechange= function(){
  74.           if(this.readyState && this.readyState=="loading")return;
  75.           callback.call();
  76.         }
  77.       }
  78.     }
  79.   }
  80. });
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}