jquery.cookie.js用法

來源:互聯網
上載者:User

標籤:

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用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.