posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          javascript cookie(轉)

          Posted on 2007-11-28 17:13 G_G 閱讀(426) 評論(0)  編輯  收藏 所屬分類: javascript
          引用
          http://blog.joycode.com/joe/archive/2005/09/06/63068.aspx
          http://www.itlearner.com/article/2006/3325.shtml


          使用方法:js


          //獲得Cookie解碼后的值
          function?GetCookieVal(offset){
          ????
          var?endstr?=?document.cookie.indexOf?(";",?offset);
          ????
          if?(endstr?==?-1)
          ????endstr?
          =?document.cookie.length;
          ????
          return?unescape(document.cookie.substring(offset,?endstr));
          }

          //設定Cookie值
          function?SetCookie(name,?value){
          ????
          var?expdate?=?new?Date();
          ????
          var?argv?=?SetCookie.arguments;
          ????
          var?argc?=?SetCookie.arguments.length;
          ????
          var?expires?=?(argc?>?2)???argv[2]?:?null;
          ????
          var?path?=?(argc?>?3)???argv[3]?:?null;
          ????
          var?domain?=?(argc?>?4)???argv[4]?:?null;
          ????
          var?secure?=?(argc?>?5)???argv[5]?:?false;
          ????
          if(expires!=null)?expdate.setTime(expdate.getTime()?+?(?expires?*?1000?));
          ????document.cookie?
          =?name?+?"="?+?escape?(value)?+((expires?==?null)???""?:?(";?expires="+?expdate.toGMTString()))
          ????
          +((path?==?null)???""?:?(";?path="?+?path))?+((domain?==?null)???""?:?(";?domain="?+?domain))
          ????
          +((secure?==?true)???";?secure"?:?"");
          }

          //刪除Cookie
          function?DelCookie(name){
          ????
          var?exp?=?new?Date();
          ????exp.setTime?(exp.getTime()?
          -?1);
          ????
          var?cval?=?GetCookie?(name);
          ????document.cookie?
          =?name?+?"="?+?cval?+?";?expires="+?exp.toGMTString();
          }

          //獲得Cookie的原始值
          function?GetCookie(name){
          ????
          var?arg?=?name?+?"=";
          ????
          var?alen?=?arg.length;
          ????
          var?clen?=?document.cookie.length;
          ????
          var?i?=?0;
          ????
          while?(i?<?clen)
          ????{
          ????????
          var?j?=?i?+?alen;
          ????????
          if?(document.cookie.substring(i,?j)?==?arg)
          ????????
          return?GetCookieVal?(j);
          ????????i?
          =?document.cookie.indexOf("?",?i)?+?1;
          ????????
          if?(i?==?0)?break;
          ????}
          ????
          return?null;
          }

          jsunit
          <!DOCTYPE?html?
          ??PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?
          ??"http://www.w3.org/TR/html4/loose.dtd"
          >
          <html>
          <head>
          <meta?http-equiv="Content-Type"?content="text/html;?charset=BIG5">
          <title>x</title>
          <script?type="text/javascript"?src="test.js"></script>
          <script?type="text/javascript"?src="lib/jsUnitCore.js"></script>
          <script?type="text/javascript">
          ????
          ????
          ????
          function?testValidArgs()?{?//?測試的函式要以test名稱作為開頭
          ????????SetCookie('li','gg');
          ????????assertEquals(
          "cookie?get?err",?'gg',?GetCookie('li')?);
          ????????assertEquals(
          "cookie?getVal?err",?'li=gg',?GetCookieVal('li')?);
          ????????
          ????????
          ????????DelCookie(
          "li");
          ????????assertEquals(
          "cookie del err ",null,?GetCookie('li')?);
          ????????
          ????????
          ????}

          ????
          ????
          </script>
          </head>
          <body>
          </body>
          </html>

          運行 Jsunit-> testRunner.html 通過測試


          主站蜘蛛池模板: 板桥市| 永登县| 乌拉特前旗| 滨州市| 游戏| 门头沟区| 平果县| 太湖县| 利津县| 靖远县| 青阳县| 湘潭市| 伊宁市| 子长县| 天镇县| 安庆市| 进贤县| 福州市| 江源县| 绩溪县| 定兴县| 天全县| 通山县| 哈尔滨市| 松溪县| 洛扎县| 大关县| 桐城市| 温宿县| 靖江市| 大方县| 太康县| 锦州市| 汉中市| 通河县| 商城县| 清新县| 广汉市| 嘉黎县| 福泉市| 天全县|