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

首頁 > 語言 > JavaScript > 正文

JavaScript操作Cookie方法實例分析

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

這篇文章主要介紹了JavaScript操作Cookie方法,實例分析了javascript針對cookie操作的相關技巧,需要的朋友可以參考下

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

 

 
  1. // My methods for setting, reading and deleting cookies. 
  2. // I have methods to check for the existence of cookie names or values,  
  3. // to retrieve by name or value, and to create a formatted string of  
  4. // all the cookies. 
  5. // My site: andrew.dx.am 
  6. var SetCookie = function (name, value, expires, path, domain, secure) { 
  7. // The caller should Trim the name/value pair, if required. 
  8. // Sets the name/value pair (encoded); 'expires' is the no. of days. 
  9. var expires_date; 
  10. if (expires) { 
  11. expires_date = new Date(); 
  12. expires_date.setDate(expires_date.getDate() + expires); 
  13. document.cookie = encodeURIComponent(name) + "=" +  
  14. encodeURIComponent(value) + 
  15. ( ( expires ) ? ";expires=" + expires_date.toUTCString() : "" ) + 
  16. ( ( path ) ? ";path=" + path : "" ) + 
  17. ( ( domain ) ? ";domain=" + domain : "" ) + 
  18. ( ( secure ) ? ";secure" : "" ); 
  19. }; 
  20. var DeleteCookie = function (name, path, domain) { 
  21. // The caller should Trim the name/value pair. 
  22. // Encodes the name before deleting. 
  23. document.cookie = encodeURIComponent(name) + "=" +  
  24. ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" +  
  25. domain : "" ) + ";expires=Fri, 01-Jan-2010 00:00:01 UTC"
  26. }; 
  27. var DelAllCookies = function () { 
  28. var currDate = new Date(), i, theCookie = document.cookie.split(";"); 
  29. currDate = currDate.toUTCString(); 
  30. i = theCookie.length; 
  31. while ( i-- ) { 
  32. document.cookie = theCookie[i] + "; expires =" + currDate; 
  33. }; 
  34. var EscapeReg = function (str) { 
  35. // Helper fn: Escapes characters for use in a regular expression. 
  36. return str.replace(/[-[/]{}()*+?.,//^$|#/s]/g, "//#FormatTableID_0#amp;"); 
  37. }; 
  38. // The following four functions do not Trim the name or value  
  39. // - the calling fns should do this. 
  40. var CNameExists = function (cookie_name) { // case-insensitive 
  41. var testName, myReg; 
  42. if (document.cookie.length == 0) return false
  43. testName = EscapeReg(cookie_name); 
  44. myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); 
  45. return myReg.test(decodeURIComponent(document.cookie)); 
  46. }; 
  47. var CValueExists = function (cookie_value) { // case insensitive 
  48. var testName, myReg; 
  49. if (document.cookie.length == 0) return false;  
  50. testName = EscapeReg(cookie_value); 
  51. myReg = new RegExp('(=)' + testName + '(;|$)','i'); 
  52. return myReg.test(decodeURIComponent(document.cookie)); 
  53. }; 
  54. var CNameGet = function (cookie_value) { // case-insensitive 
  55. var testName, myReg, results; 
  56. if (document.cookie.length == 0) return ''
  57. testName = EscapeReg(cookie_value); 
  58. myReg = new RegExp('(^|;) ?([^=]*)=' + testName + '(;|$)','i'); 
  59. results = decodeURIComponent(document.cookie).match(myReg); 
  60. return ( results ) ? results[2] : ''
  61. }; 
  62. var CValueGet = function (cookie_name) { // case-insensitive 
  63. var testName, myReg, results; 
  64. if (document.cookie.length == 0) return ''
  65. testName = EscapeReg(cookie_name); 
  66. myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); 
  67. results = decodeURIComponent(document.cookie).match(myReg); 
  68. return ( results ) ? results[2] : ''
  69. }; 
  70. var CookieStr = function () { 
  71. // Returns a string (with line breaks) which could be  
  72. // placed in, for example, a textarea. 
  73. return decodeURIComponent(document.cookie). 
  74. replace(/([^=;]+)=([^;]*)[;/s]*/g,'$1 ($2)/n') || ''
  75. }; 

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

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

圖片精選

主站蜘蛛池模板: 巧家县| 丹东市| 仁寿县| 东港市| 渭源县| 房产| 康定县| 富锦市| 门头沟区| 仁寿县| 青河县| 化隆| 乌海市| 茌平县| 五指山市| 青铜峡市| 太保市| 晋宁县| 镇赉县| 灵寿县| 云霄县| 新丰县| 房产| 涞源县| 华安县| 德江县| 高邑县| 宁城县| 甘南县| 新化县| 丽江市| 东莞市| 栾川县| 南昌县| 尚义县| 阳谷县| 福清市| 申扎县| 屯昌县| 叙永县| 岳阳县|