首页 > 语言 > 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. } 
  14. document.cookie = encodeURIComponent(name) + "=" +  
  15. encodeURIComponent(value) + 
  16. ( ( expires ) ? ";expires=" + expires_date.toUTCString() : "" ) + 
  17. ( ( path ) ? ";path=" + path : "" ) + 
  18. ( ( domain ) ? ";domain=" + domain : "" ) + 
  19. ( ( secure ) ? ";secure" : "" ); 
  20. }; 
  21. var DeleteCookie = function (name, path, domain) { 
  22. // The caller should Trim the name/value pair. 
  23. // Encodes the name before deleting. 
  24. document.cookie = encodeURIComponent(name) + "=" +  
  25. ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" +  
  26. domain : "" ) + ";expires=Fri, 01-Jan-2010 00:00:01 UTC"; 
  27. }; 
  28. var DelAllCookies = function () { 
  29. var currDate = new Date(), i, theCookie = document.cookie.split(";"); 
  30. currDate = currDate.toUTCString(); 
  31. i = theCookie.length; 
  32. while ( i-- ) { 
  33. document.cookie = theCookie[i] + "; expires =" + currDate; 
  34. } 
  35. }; 
  36. var EscapeReg = function (str) { 
  37. // Helper fn: Escapes characters for use in a regular expression. 
  38. return str.replace(/[-[/]{}()*+?.,//^$|#/s]/g, "//#FormatTableID_0#amp;"); 
  39. }; 
  40. // The following four functions do not Trim the name or value  
  41. // - the calling fns should do this. 
  42. var CNameExists = function (cookie_name) { // case-insensitive 
  43. var testName, myReg; 
  44. if (document.cookie.length == 0) return false; 
  45. testName = EscapeReg(cookie_name); 
  46. myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); 
  47. return myReg.test(decodeURIComponent(document.cookie)); 
  48. }; 
  49. var CValueExists = function (cookie_value) { // case insensitive 
  50. var testName, myReg; 
  51. if (document.cookie.length == 0) return false;  
  52. testName = EscapeReg(cookie_value); 
  53. myReg = new RegExp('(=)' + testName + '(;|$)','i'); 
  54. return myReg.test(decodeURIComponent(document.cookie)); 
  55. }; 
  56. var CNameGet = function (cookie_value) { // case-insensitive 
  57. var testName, myReg, results; 
  58. if (document.cookie.length == 0) return ''; 
  59. testName = EscapeReg(cookie_value); 
  60. myReg = new RegExp('(^|;) ?([^=]*)=' + testName + '(;|$)','i'); 
  61. results = decodeURIComponent(document.cookie).match(myReg); 
  62. return ( results ) ? results[2] : ''; 
  63. }; 
  64. var CValueGet = function (cookie_name) { // case-insensitive 
  65. var testName, myReg, results; 
  66. if (document.cookie.length == 0) return ''; 
  67. testName = EscapeReg(cookie_name); 
  68. myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); 
  69. results = decodeURIComponent(document.cookie).match(myReg); 
  70. return ( results ) ? results[2] : ''; 
  71. }; 
  72. var CookieStr = function () { 
  73. // Returns a string (with line breaks) which could be  
  74. // placed in, for example, a textarea. 
  75. return decodeURIComponent(document.cookie). 
  76. replace(/([^=;]+)=([^;]*)[;/s]*/g,'$1 ($2)/n') || ''; 
  77. }; 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选