rev |
line source |
paulo@78
|
1 /*\
|
paulo@78
|
2 |*|
|
paulo@78
|
3 |*| :: cookies.js ::
|
paulo@78
|
4 |*|
|
paulo@78
|
5 |*| A complete cookies reader/writer framework with full unicode support.
|
paulo@78
|
6 |*|
|
paulo@78
|
7 |*| Revision #1 - September 4, 2014
|
paulo@78
|
8 |*|
|
paulo@78
|
9 |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|
paulo@78
|
10 |*| https://developer.mozilla.org/User:fusionchess
|
paulo@78
|
11 |*|
|
paulo@78
|
12 |*| This framework is released under the GNU Public License, version 3 or later.
|
paulo@78
|
13 |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|
paulo@78
|
14 |*|
|
paulo@78
|
15 |*| Syntaxes:
|
paulo@78
|
16 |*|
|
paulo@78
|
17 |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|
paulo@78
|
18 |*| * docCookies.getItem(name)
|
paulo@78
|
19 |*| * docCookies.removeItem(name[, path[, domain]])
|
paulo@78
|
20 |*| * docCookies.hasItem(name)
|
paulo@78
|
21 |*| * docCookies.keys()
|
paulo@78
|
22 |*|
|
paulo@78
|
23 \*/
|
paulo@78
|
24
|
paulo@78
|
25 var docCookies = {
|
paulo@78
|
26 getItem: function (sKey) {
|
paulo@78
|
27 if (!sKey) { return null; }
|
paulo@78
|
28 return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
paulo@78
|
29 },
|
paulo@78
|
30 setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
|
paulo@78
|
31 if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
|
paulo@78
|
32 var sExpires = "";
|
paulo@78
|
33 if (vEnd) {
|
paulo@78
|
34 switch (vEnd.constructor) {
|
paulo@78
|
35 case Number:
|
paulo@78
|
36 sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
|
paulo@78
|
37 break;
|
paulo@78
|
38 case String:
|
paulo@78
|
39 sExpires = "; expires=" + vEnd;
|
paulo@78
|
40 break;
|
paulo@78
|
41 case Date:
|
paulo@78
|
42 sExpires = "; expires=" + vEnd.toUTCString();
|
paulo@78
|
43 break;
|
paulo@78
|
44 }
|
paulo@78
|
45 }
|
paulo@78
|
46 document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
paulo@78
|
47 return true;
|
paulo@78
|
48 },
|
paulo@78
|
49 removeItem: function (sKey, sPath, sDomain) {
|
paulo@78
|
50 if (!this.hasItem(sKey)) { return false; }
|
paulo@78
|
51 document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
|
paulo@78
|
52 return true;
|
paulo@78
|
53 },
|
paulo@78
|
54 hasItem: function (sKey) {
|
paulo@78
|
55 if (!sKey) { return false; }
|
paulo@78
|
56 return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
paulo@78
|
57 },
|
paulo@78
|
58 keys: function () {
|
paulo@78
|
59 var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
|
paulo@78
|
60 for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
|
paulo@78
|
61 return aKeys;
|
paulo@78
|
62 }
|
paulo@78
|
63 };
|