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;
  }
};
相關文章
相關標籤/搜索