日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁編程開發(fā)其它知識 → 使用userData兼容IE6-10,chrome,F(xiàn)F 及360等瀏覽器的本地存儲

      使用userData兼容IE6-10,chrome,F(xiàn)F 及360等瀏覽器的本地存儲

      前往專題相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時(shí)間:2013/3/11 18:05:36字體大。A-A+

      作者:西西點(diǎn)擊:0次評論:0次標(biāo)簽: 瀏覽器

      07073游戲?yàn)g覽器v2.0.1.6 官方正式版
      • 類型:瀏覽器類大。3.7M語言:中文 評分:6.4
      • 標(biāo)簽:
      立即下載

      開發(fā)過程中涉及本地存儲的使用,IE很多版本都不支持localStorage,沒辦法,就得兼容使用userData了。廢話不說了,看代碼:

      (function(window){
      var LS;
      (function(){

      var o = document.getElementsByTagName("head")[0],
      n = window.location.hostname || "localStorage",
      d = new Date(),doc,agent;
      //typeof o.addBehavior 在IE6下是object,在IE10下是function,因此這里直接用!判斷
      if(!o.addBehavior)return;//防止有些瀏覽器默認(rèn)禁用localStorage,這里優(yōu)先考慮userData本地存儲
      try{ //嘗試創(chuàng)建iframe代理
      agent = new ActiveXObject('htmlfile');
      agent.open();
      agent.write('<s' + 'cript>document.w=window;</s' + 'cript><iframe src="/favicon.ico"></frame>');
      agent.close();
      doc = agent.w.frames[0].document;
      }catch(e){doc = document;}
      o = doc.createElement('head');//這里通過代理document創(chuàng)建head,可以使存儲數(shù)據(jù)垮目錄訪問
      doc.appendChild(o);
      d.setDate(d.getDate() + 36500);
      o.addBehavior("#default#userData");
      o.expires = d.toUTCString();
      o.load(n);

      var root = o.XMLDocument.documentElement,
      attrs = root.attributes,
      prefix = "prefix_____hack__",
      reg1 = /^[-\d]/,
      reg2 = new RegExp("^"+prefix),
      encode = function(key){
      return reg1.test(key) ? prefix + key : key;
      },
      decode = function(key){
      return key.replace(reg2,"");
      };

      LS= {
      length: attrs.length,
      notNativeCode: true,
      getItem: function(key){
      return (attrs.getNamedItem( encode(key) ) || {nodeValue: null}).nodeValue||root.getAttribute(encode(key)); //IE9中 通過o.getAttribute(name);取不到值,所以才用了下面比較復(fù)雜的方法。(也許你會(huì)詫異IE9不是有原生的localStorage嗎,是的,但是用戶可以關(guān)閉DOM存儲,所以為了保險(xiǎn)一些還是考慮IE9可能會(huì)使用到#userData吧。)
      },
      setItem: function(key, value){
      root.setAttribute( encode(key), value); //IE9中無法通過 o.setAttribute(name, value); 設(shè)置#userData值,而用下面的方法卻可以。
      o.save(n);
      this.length = attrs.length;
      },
      removeItem: function(key){
      root.removeAttribute( encode(key) ); //IE9中無法通過 o.removeAttribute(name); 刪除#userData值,而用下面的方法卻可以。
      o.save(n);
      this.length = attrs.length;
      },
      clear: function(){
      while(attrs.length){
      this.removeItem( attrs[0].nodeName );
      }
      this.length = 0;
      },
      key: function(i){
      return attrs[i] ? decode(attrs[i].nodeName) : undefined;
      }
      };
      })();

      (function(w,localStorage){//封裝LS,對外提供接口
      var f = function(){return null;};
      w.LS = localStorage?{
      set : function(key, value){
      //fixed iPhone/iPad 'QUOTA_EXCEEDED_ERR' bug
      if( this.get(key) !== undefined )
      this.remove(key);
      localStorage.setItem(key, value);
      },
      //查詢不存在的key時(shí),有的瀏覽器返回null,這里統(tǒng)一返回undefined
      get : function(key){
      var v = localStorage.getItem(key);
      return v === null ? undefined : v;
      },
      remove : function(key){ localStorage.removeItem(key); },
      clear : function(){ localStorage.clear(); },
      each : function(callback){
      var list = this.obj(), fn = callback || function(){}, key;
      for(key in list)
      if( fn.call(this, key, this.get(key)) === false )
      break;
      },
      obj : function(){
      var list={}, i=0, n, key;
      if( localStorage.isVirtualObject ){
      list = localStorage.key(-1);
      }else{
      n = localStorage.length;
      for(; i<n; i++){
      key = localStorage.key(i);
      list[key] = this.get(key);
      }
      }
      return list;
      }
      }:{set:f,get:f,remove:f,clear:f,each:f,obj:f};//如果都不支持則所有方法返回null
      })(window,LS||window.localStorage);//這里優(yōu)先使用自定義的LS
      })(window);

      使用方法很簡單:

      對外提供全局變量LS

      存儲:LS.set('userName',"js明哥哥");

      讀。篖S.get('userName');

      刪除:LS.remove('userName');

      該組件經(jīng)過本人測試,暫時(shí)沒發(fā)現(xiàn)什么瀏覽器不支持,可能測試還不夠到位,希望大家多多指點(diǎn)。

        游戲?yàn)g覽器
        (53)游戲?yàn)g覽器
        西西軟件園提供最好用的游戲?yàn)g覽器,游戲?yàn)g覽器是專門針對網(wǎng)頁游戲而設(shè)計(jì)的專用瀏覽器,游戲?yàn)g覽器能夠給玩家提供加速功能,還有各種輔助工具,最終要是支持快捷按鍵.網(wǎng)絡(luò)上有很多游戲?yàn)g覽器,到底哪個(gè)游戲?yàn)g覽器最好用呢西西小編就整理出了最好用的瀏覽器排行榜給大家選擇....更多>>

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)
        推薦文章

        沒有數(shù)據(jù)