js cookie 浏览器 document

用 js 操作浏览器的时候 多加了几个cookie  ,结果  删除了半天没有效果 ,网上查了半天 ,document.cookie=‘’  这种方式是没有用的  ,cookie  不是字符串  ,是key -value  键值对,可以用字符串的方式 方便的赋值。详情见一下的mdn 链接 。查了一个小时 还不如 官方的额说明 来的快 权威。

建议大家 多上 官网 上查。

https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie


/*\
|*|
|*|  :: cookies.js ::
|*|
|*|  A complete cookies reader/writer framework with full unicode support.
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntaxes:
|*|
|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*|  * docCookies.getItem(name)
|*|  * docCookies.removeItem(name[, path], domain)
|*|  * docCookies.hasItem(name)
|*|  * docCookies.keys()
|*|
\*/

var docCookies = {
  getItem: function (sKey) {
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  },
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    return true;
  },
  removeItem: function (sKey, sPath, sDomain) {
    if (!sKey || !this.hasItem(sKey)) { return false; }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
    return true;
  },
  hasItem: function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  },
  keys: /* optional method: you can safely remove it! */ function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    return aKeys;
  }
};

写入cookie

语法
docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
描述

创建或覆盖一个cookie

参数
name (必要)
要创建或覆盖的cookie的名字 ( string)。
value (必要)
cookie的值 ( string)。
end  (可选)
最大年龄的秒数 (一年为31536e3, 永不过期的cookie为 Infinity) ,或者过期时间的 GMTString格式或 Date对象; 如果没有定义则会在会话结束时过期 ( number – 有限的或  Infinity –  stringDate object or  null)。
path  (可选)
例如 '/', '/mydir'。 如果没有定义,默认为当前文档位置的路径。( string or  null)。路径必须为绝对路径(参见  RFC 2965)。关于如何在这个参数使用相对路径的方法请参见 这段
domain  (可选)
例如 'example.com', '.example.com' (包括所有子域名), 'subdomain.example.com'。如果没有定义,默认为当前文档位置的路径的域名部分 ( string null)。
secure  (可选)
cookie只会被https传输 ( booleannull)。

得到cookie

语法
docCookies.getItem(name)
描述

读取一个cookie。如果cookie不存在返回null

参数
name
读取的cookie名 ( string).

移除cookie

Syntax
docCookies.removeItem(name[, path],domain)
描述

删除一个cookie。

参数
name
要移除的cookie名( string).
path  (可选 )
例如 '/', '/mydir'。 如果没有定义,默认为当前文档位置的路径。( string or  null)。路径必须为绝对路径(参见  RFC 2965)。关于如何在这个参数使用相对路径的方法请参见 这段
domain (可选)
例如 'example.com', '.example.com' (包括所有子域名), 'subdomain.example.com'。如果没有定义,默认为当前文档位置的路径的域名部分 ( string null)。

检测cookie

语法
docCookies.hasItem(name)
描述

检查一个cookie是否存在

参数
name
要检查的cookie名 ( string).

得到所有cookie的列表

语法
docCookies.keys()
描述

返回一个这个路径所有可读的cookie的数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值