cookie外掛程式|jq-cookie.js|使用詳解

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   使用   sp   

1、設定一次層網域共用的cookie:設定domain為頂層網域,可一、次層網域共用的cookie

$.cookie(‘f_city‘,‘北京|101010100|,錦州|101070701|‘,{expires: 999, path: ‘/‘,domain: ‘weather.com.cn‘})

 

2、刪除同名不同域的cookie (一個頁面中.weather.com.cn域 和 www.weather.com.cn域下,都有f_city這個值,怎麼刪除其中任一)

$.cookie(‘f_city‘,null,{ path: ‘/‘})   //刪除當前域 www.weather.com.cn域下的 f_city$.cookie(‘f_city‘,null,{ path: ‘/‘, domain: ‘weather.com.cn‘})   //刪除 .weather.com.cn域下的 f_city

 

3、讀取cookie:$.cookie(‘f_city‘),當讀取cookie索引值為 f_city 的值時,如果有不同域下的多個f_city,則讀取建立最早的f_city值,而與域無關

var cookie_f_city= $.cookie(‘f_city‘); //讀取cookie只有一種寫法,不接受{domain}參數,也就是說,當有域同名參數時,唯讀建立最早的 f_city

 

 

 

=======================

下面是jq-cookie這個外掛程式的源碼

/** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * *//** * *1、設定cookie的值,比如我們要設定變數名為userid對應值為123的cookie,代碼如下: * $.cookie(‘userid‘,‘123‘); * *2、建立一個cookie,並設定cookie的有效期間 路徑 網域名稱等,代碼如下: * $.cookie(‘userid, ‘123‘, {expires: 7, path: ‘/‘, domain: ‘jquery.com‘, secure: true});注意:如果去掉後面{}的參數,建立後將以預設設定生效。 * *3、刪除cookie,即把對應cookie值置為null,代碼如下: * $.cookie(‘userid‘, null); * *4、讀取cookie,如讀取變數名為userid的cookie值,代碼如下: * var uId= $.cookie(‘userid‘); * *//** * Get the value of a cookie with the given name. * * @example $.cookie(‘the_cookie‘); * @desc Get the value of a cookie. * * @param String name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/[email protected] */jQuery.cookie = function(name, value, options) {    if (typeof value != ‘undefined‘) { // name and value given, set cookie        options = options || {};        if (value === null) {            value = ‘‘;            options.expires = -1;        }        var expires = ‘‘;        if (options.expires && (typeof options.expires == ‘number‘ || options.expires.toUTCString)) {            var date;            if (typeof options.expires == ‘number‘) {                date = new Date();                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));            } else {                date = options.expires;            }            expires = ‘; expires=‘ + date.toUTCString(); // use expires attribute, max-age is not supported by IE        }        var path = options.path ? ‘; path=‘ + options.path : ‘‘;        var domain = options.domain ? ‘; domain=‘ + options.domain : ‘‘;        var secure = options.secure ? ‘; secure‘ : ‘‘;        document.cookie = [name, ‘=‘, encodeURIComponent(value), expires, path, domain, secure].join(‘‘);    } else { // only name given, get cookie        var cookieValue = null;        if (document.cookie && document.cookie != ‘‘) {            var cookies = document.cookie.split(‘;‘);            for (var i = 0; i < cookies.length; i++) {                var cookie = jQuery.trim(cookies[i]);                // Does this cookie string begin with the name we want?                if (cookie.substring(0, name.length + 1) == (name + ‘=‘)) {                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));                    break;                }            }        }        return cookieValue;    }};

 

var cookie_f_city= $.cookie(‘f_city‘); //讀取cookie只有一種寫法,不接受{domain}參數,也就是說,當有域同名參數時,唯讀建立最早的 f_city

cookie外掛程式|jq-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.