[JavaScript] JavaScript操作Cookie演示代码 →→→→→进入此内容的聊天室

来自 , 2019-10-17, 写在 JavaScript, 查看 97 次.
URL http://www.code666.cn/view/39252609
  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, "\\$&");
  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. };
  78. //javascript/5212

回复 "JavaScript操作Cookie演示代码"

这儿你可以回复上面这条便签

captcha