document.cookie

MDN上给出了一个处理document.cookie的小插件html

/*\
|*|
|*|  :: 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
|*|  https://github.com/madmurphy/cookies.js
|*|
|*|  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) {//获取cookie
    if (!sKey) { return null; }//若是没有传递cookie的名字,返回null
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
    //使用正则获取到sKey对应的cookie值,若是没有返回null
  },
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {//设置新cookie
    //sKey,cookie名字;sValue,cookie值;vEnd,cookie过时时间;sPath,cookie路径;sDomain,cookie所在域;bSecure,是否只被https传输
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    //若是没有传递cookie名字或者名字是特殊单词,返回false
    var sExpires = "";
    if (vEnd) {//若是传递了过时时间
      switch (vEnd.constructor) {//判断过时时间参数的类型
        case Number:
          //若是是数字类型,若是是永久cookie,使用31 Dec 9999 23:59:59 GMT做为expires过时日期,不然使用max-age设置生存时间有多长
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          //若是是字符串,使用expires过时日期
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          //若是是日期对象,使用expires过时日期
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    //将各个配置项字符串链接起来,赋值给document.cookie
    return true;
  },
  removeItem: function (sKey, sPath, sDomain) {//移除cookie
    if (!this.hasItem(sKey)) { return false; }//若是没有此cookie返回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) {//检测是否有某个cookie
    if (!sKey) { return false; }//若是没有传递cookie名字,返回false
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);//用正则判断是否有sKey对应cookie
  },
  keys: function () {//获取全部cookie名字组成的数组
    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;
  }
};
相关文章
相关标签/搜索