国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript操作cookie類實例

2024-05-06 16:17:51
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript操作cookie類,實例分析了javascript針對cookie的設置、讀取、刪除等常用技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了JavaScript操作cookie類。分享給大家供大家參考。具體如下:

用法:

一、設置cookie

 

 
  1. var cookie = new JSCookie(); 
  2. // 普通設置 
  3. cookie .SetCookie("key1","val1"); 
  4. // 過期時間為一年 
  5. var expire_time = new Date(); 
  6. expire_time.setFullYear(expire_time.getFullYear() + 1); 
  7. cookie .SetCookie("key2","val2",expire_time); 
  8. // 設置域及路徑,帶過期時間 
  9. cookie .SetCookie("key3","val3",expire_time,".cnblogs.com","/"); 
  10. // 設置帶子鍵的cookie,子鍵分別是k1,k2,k3 
  11. cookie .SetCookie("key4","k1=1&k2=2&k3=3"); 

二、讀取cookie

 

 
  1. // 簡單獲取 
  2. cookie .GetCookie("key1"); 
  3. cookie .GetCookie("key2"); 
  4. cookie .GetCookie("key3"); 
  5. cookie .GetCookie("key4"); 
  6. // 獲取key4的子鍵k1值 
  7. cookie .GetChild("key4","k1"); 

三、刪除

 

 
  1. cookie .Expire("key1"); 
  2. cookie .Expire("key2"); 
  3. cookie .Expire("key3"); 
  4. cookie .Expire("key4"); 

示例:

 

 
  1. <script type="text/javascript"
  2. String.prototype.Trim = function() 
  3. return this.replace(/^/s+/g,"").replace(//s+$/g,""); 
  4. function JSCookie() 
  5. this.GetCookie = function(key) 
  6. var cookie = document.cookie; 
  7. var cookieArray = cookie.split(';'); 
  8. var getvalue = ""
  9. for(var i = 0;i<cookieArray.length;i++) 
  10. if(cookieArray[i].Trim().substr(0,key.length) == key) 
  11. getvalue = cookieArray[i].Trim().substr(key.length + 1); 
  12. break
  13. return getvalue; 
  14. }; 
  15. this.GetChild = function(cookiekey,childkey) 
  16. var child = this.GetCookie(cookiekey); 
  17. var childs = child.split('&'); 
  18. var getvalue = ""
  19. for(var i = 0;i < childs.length;i++) 
  20. if(childs[i].Trim().substr(0,childkey.length) == childkey) 
  21. getvalue = childs[i].Trim().substr(childkey.length + 1); 
  22. break
  23. return getvalue; 
  24. }; 
  25. this.SetCookie = function(key,value,expire,domain,path) 
  26. var cookie = ""
  27. if(key != null && value != null
  28. cookie += key + "=" + value + ";"
  29. if(expire != null
  30. cookie += "expires=" + expire.toGMTString() + ";"
  31. if(domain != null
  32. cookie += "domain=" + domain + ";"
  33. if(path != null
  34. cookie += "path=" + path + ";"
  35. document.cookie = cookie; 
  36. }; 
  37. this.Expire = function(key) 
  38. expire_time = new Date(); 
  39. expire_time.setFullYear(expire_time.getFullYear() - 1); 
  40. var cookie = " " + key + "=e;expires=" + expire_time + ";" 
  41. document.cookie = cookie; 
  42. </script> 

至此完畢.

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 天门市| 通化市| 广西| 墨玉县| 松滋市| 大埔县| 隆德县| 鄂托克旗| 女性| 肥乡县| 仙游县| 宁海县| 龙井市| 浙江省| 丰原市| 威宁| 乐亭县| 抚宁县| 自治县| 西盟| 错那县| 彭州市| 青浦区| 南木林县| 梅河口市| 永平县| 彭泽县| 墨玉县| 故城县| 英山县| 呼图壁县| 开平市| 谢通门县| 汉中市| 上林县| 灵山县| 虎林市| 巨鹿县| 普格县| 安义县| 朔州市|