paulo@78: /*\ paulo@78: |*| paulo@78: |*| :: cookies.js :: paulo@78: |*| paulo@78: |*| A complete cookies reader/writer framework with full unicode support. paulo@78: |*| paulo@78: |*| Revision #1 - September 4, 2014 paulo@78: |*| paulo@78: |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie paulo@78: |*| https://developer.mozilla.org/User:fusionchess paulo@78: |*| paulo@78: |*| This framework is released under the GNU Public License, version 3 or later. paulo@78: |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html paulo@78: |*| paulo@78: |*| Syntaxes: paulo@78: |*| paulo@78: |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]]) paulo@78: |*| * docCookies.getItem(name) paulo@78: |*| * docCookies.removeItem(name[, path[, domain]]) paulo@78: |*| * docCookies.hasItem(name) paulo@78: |*| * docCookies.keys() paulo@78: |*| paulo@78: \*/ paulo@78: paulo@78: var docCookies = { paulo@78: getItem: function (sKey) { paulo@78: if (!sKey) { return null; } paulo@78: return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; paulo@78: }, paulo@78: setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { paulo@78: if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } paulo@78: var sExpires = ""; paulo@78: if (vEnd) { paulo@78: switch (vEnd.constructor) { paulo@78: case Number: paulo@78: sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; paulo@78: break; paulo@78: case String: paulo@78: sExpires = "; expires=" + vEnd; paulo@78: break; paulo@78: case Date: paulo@78: sExpires = "; expires=" + vEnd.toUTCString(); paulo@78: break; paulo@78: } paulo@78: } paulo@78: document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); paulo@78: return true; paulo@78: }, paulo@78: removeItem: function (sKey, sPath, sDomain) { paulo@78: if (!this.hasItem(sKey)) { return false; } paulo@78: document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : ""); paulo@78: return true; paulo@78: }, paulo@78: hasItem: function (sKey) { paulo@78: if (!sKey) { return false; } paulo@78: return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); paulo@78: }, paulo@78: keys: function () { paulo@78: var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); paulo@78: for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } paulo@78: return aKeys; paulo@78: } paulo@78: };