標籤:
jQuery.cookie = function(d, c, a) { if ("undefined" != typeof c) { a = a || {}; null === c && (c = "", a = $.extend({}, a), a.expires = -1); a.expires || (a.expires = 1); var b = ""; if (a.expires && ("number" == typeof a.expires || a.expires.toUTCString))"number" == typeof a.expires ? (b = new Date, b.setTime(b.getTime() + 864E5 * a.expires)) : b = a.expires, b = "; expires=" + b.toUTCString(); var e = a.path ? "; path=" + a.path: "", f = a.domain ? "; domain=" + a.domain: "", a = a.secure ? "; secure": ""; document.cookie = [d, "=", encodeURIComponent(c), b, e, f, a].join("") } else { c = null; if (document.cookie && "" != document.cookie) { a = document.cookie.split(";"); for (b = 0; b < a.length; b++) if (e = jQuery.trim(a[b]), e.substring(0, d.length + 1) == d + "=") { c = decodeURIComponent(e.substring(d.length + 1)); break } } return c }};用法:
設定 cookie:
$.cookie(‘the_cookie‘, ‘the_value‘);
註:如果 $.cookie 沒有第三個參數,那麼當瀏覽器關閉時,該 cookie 將會自動刪除。
設定一個有效期間為 7 天的 cookie:
$.cookie(‘the_cookie‘, ‘the_value‘, {expires: 7});
註:$.cookie 第三個參數是一個對象,除了可以設定有效期間(expires: 7),還可以設定有效路徑(path: ‘/‘)、有效域(domain: ‘jquery.com‘)及安全性(secure: true)。
讀取 cookie:
$.cookie(‘the_cookie‘);
註:如果沒有該 cookie,返回 null。
刪除 cookie:
$.cookie(‘the_cookie‘, null);
我們只需要給需要刪除的 cookie 設定為 null,就可以刪除該 cookie。
jquery.cookie.js用法