2010-12-01 70 views
5

我正在使用jQuery cookie插件來讀取/寫入/刪除cookie。我使用cookie將點存儲在用戶繪製在畫布上的圖形上。我允許用戶在cookie中存儲繪製的點以及名稱,我還列出了保存的cookie,以便他們可以在圖形上重新繪製其保存的點。jQuery:如果您不知道名稱,請閱讀所有域Cookie

我最初是通過命名每個cookie的序列號$.cookie("_1")$.cookie("_2")等來保存和重新加載cookies的點,這個工作。當用戶刪除一個cookie並且連續編號中斷時,問題就開始了。

我想使用用戶給出的點名保存cookie,所以基本上用任意名稱保存cookie。如果我這樣做如果我不知道他們的名字,是否可以讀取所有的域名cookie?

回答

12

你不需要這個jQuery。您可以通過訪問document.cookie並相應地解析來閱讀所有Cookie。

查看示例here

2

我不知道如何調用所有域cookie,但是您可以隨時使用您選擇的名稱來存儲cookie,然後使cookie的值爲「name,x,y」或其他。只是一個想法,作爲嘗試提取所有域cookie的替代方案。

編輯:

此外,this cookies plugin wiki表明,你可以很容易地得到餅乾的過濾列表。因此,您可以在cookie的名稱上輸入標識符"mysite+name",然後在獲得過濾列表後使用.slice將其取消。

+0

呀,能做到這一點,是要改寫餅乾的順序編號,如果所有的人的閱讀是不可能的。 – 2010-12-01 14:35:57

+0

我發現了一些可能有用的東西,所以我編輯了我的答案以顯示它。 – Chris 2010-12-01 14:44:02

0

除了已經提供的答案外,您可以簡單地使用Mozilla的人員在他們的Document.cookie文檔中創建的框架。

下面是它的外觀:

/*\ 
|*| 
|*| :: cookies.js :: 
|*| 
|*| A complete cookies reader/writer framework with full unicode support. 
|*| 
|*| Revision #1 - September 4, 2014 
|*| 
|*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie 
|*| https://developer.mozilla.org/User:fusionchess 
|*| 
|*| 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) { 
    if (!sKey) { return null; } 
    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 (!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) { 
    if (!sKey) { return false; } 
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); 
    }, 
    keys: function() { 
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); 
    for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } 
    return aKeys; 
    } 
}; 

來源:developer.mozilla.org - Document.cookie - A little framework: a complete cookies reader/writer with full unicode support

相關問題