cookies.js
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:2k
源码类别:

Ajax

开发平台:

Others

  1. /**
  2.  * Sets a Cookie with the given name and value.
  3.  *
  4.  * name       Name of the cookie
  5.  * value      Value of the cookie
  6.  * [expires]  Expiration date of the cookie (default: end of current session)
  7.  * [path]     Path where the cookie is valid (default: path of calling document)
  8.  * [domain]   Domain where the cookie is valid
  9.  *              (default: domain of calling document)
  10.  * [secure]   Boolean value indicating if the cookie transmission requires a
  11.  *              secure transmission
  12.  */
  13. function setCookie(name, value, expires, path, domain, secure) {
  14.   document.cookie= name + "=" + escape(value) +
  15.     ((expires) ? "; expires=" + expires.toGMTString() : "") +
  16.     ((path) ? "; path=" + path : "") +
  17.     ((domain) ? "; domain=" + domain : "") +
  18.     ((secure) ? "; secure" : "");
  19. }
  20. /**
  21.  * Gets the value of the specified cookie.
  22.  *
  23.  * name  Name of the desired cookie.
  24.  *
  25.  * Returns a string containing value of specified cookie,
  26.  *   or null if cookie does not exist.
  27.  */
  28. function getCookie(name) {
  29.   var dc = document.cookie;
  30.   var prefix = name + "=";
  31.   var begin = dc.indexOf("; " + prefix);
  32.   if (begin == -1) {
  33.     begin = dc.indexOf(prefix);
  34.     if (begin != 0) return null;
  35.   } else {
  36.     begin += 2;
  37.   }
  38.   var end = document.cookie.indexOf(";", begin);
  39.   if (end == -1) {
  40.     end = dc.length;
  41.   }
  42.   return unescape(decodeURI(dc.substring(begin + prefix.length, end))).split("+").join(" ");
  43. }
  44. /**
  45.  * Deletes the specified cookie.
  46.  *
  47.  * name      name of the cookie
  48.  * [path]    path of the cookie (must be same as path used to create cookie)
  49.  * [domain]  domain of the cookie (must be same as domain used to create cookie)
  50.  */
  51. function deleteCookie(name, path, domain) {
  52.   if (getCookie(name)) {
  53.     document.cookie = name + "=" + 
  54.       ((path) ? "; path=" + path : "") +
  55.       ((domain) ? "; domain=" + domain : "") +
  56.       "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  57.   }
  58. }