为什么我不能从开发工具控制台中删除此cookie?

时间:2013-09-24 18:42:18

标签: javascript cookies

我有一些控制网格中列布局的cookie。它们由页面上的Javascript设置,服务器与它们无关。我想使用JS中的按钮清除它们(重置为默认值)。问题是它们似乎无法删除,除非您使用浏览器GUI来执行此操作。

我阅读了很多帖子,尝试了所有帖子,甚至是https://developer.mozilla.org/en-US/docs/Web/API/document.cookie及其docCookies库。

以下是开发工具控制台的成绩单,其中显示了这些Cookie的顽强程度:

>document.cookie="food=hotdog"
 "food=hotdog"
>docCookies.keys()
 ["food", "ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("ext-history-results")
 true
>docCookies.keys()
 ["food", "ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("food")
 true
>docCookies.keys()
 ["ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("ext-history-results")
 true
>docCookies.keys()
 ["ext-history-results", "ext-today-summaries", "csrftoken"]
>document.cookie="food=hotdog"
 "food=hotdog"
>docCookies.keys()
 ["food", "ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("ext-history-results")
 true
>docCookies.keys()
 ["food", "ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("food")
 true
>docCookies.keys()
 ["ext-history-results", "ext-today-summaries", "csrftoken"]  //csrftoken is set by the server
>docCookies.removeItem("csrftoken")
 true
>docCookies.keys()
 ["ext-history-results", "ext-today-summaries", "csrftoken"]
>docCookies.removeItem("ext-today-summaries")
 true
>document.cookie          // output wrapped for "clarity"
"ext-history-results=o%3Acolumns%3Da%253Ao%25253Aid%25253Ds%2525253Ah9%255Eo%25253Aid%25253Ds%2525253Aext-gen1133%255Eo%25253Aid%25253Ds%2525253Ah1%255Eo%25253Aid%25253Ds%2525253Ah2%255Eo%25253Aid%25253Ds%2525253Ah3%255Eo%25253Aid%25253Ds%2525253Ah4%255Eo%25253Aid%25253Ds%2525253Ah5%255Eo%25253Aid%25253Ds%2525253Ah6%255Eo%25253Aid%25253Ds%2525253Ah7%255Eo%25253Aid%25253Ds%2525253Ah8%5Esort%3Do%253Aproperty%253Ds%25253Agrouper%255Edirection%253Ds%25253AASC%255Eroot%253Ds%25253Adata; 
 ext-today-summaries=o%3Acolumns%3Da%253Ao%25253Aid%25253Ds%2525253Ah1%255Eo%25253Aid%25253Ds%2525253Ah2%255Eo%25253Aid%25253Ds%2525253Ah3%255Eo%25253Aid%25253Ds%2525253Ah4%255Eo%25253Aid%25253Ds%2525253Ah5%255Eo%25253Aid%25253Ds%2525253Ah6%255Eo%25253Aid%25253Ds%2525253Ah7%255Eo%25253Aid%25253Ds%2525253Ah8%255Eo%25253Aid%25253Ds%2525253Ah9%5Esort%3Do%253Aproperty%253Ds%25253ABox%255Edirection%253Ds%25253AASC%255Eroot%253Ds%25253Adata; 
 csrftoken=RMYlasyNxjPz8pMpn2gMvYxCscMQGX5W"

为什么我不能删除这些cookie?

docCookies代码如下(它也在上面的链接上,但是如果链接发生变化)

/*\
|*|
|*|  :: 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;
  }
};

1 个答案:

答案 0 :(得分:2)

发布此问题后,我在页面右侧看到了此链接

Why can't I delete this cookie?

第一个答案(来自Andreas Peterson)得到了答案:

docCookies.removeItem("ext-today-summaries", '/')

将删除Cookie。必须指定路径,否则它不会被删除。