jQuery 操作Cookie

來源:互聯網
上載者:User

jQuery.cookie = function(name, value, options) {
          if (typeof value != 'undefined') {
                    options = options || {};
                    if (value === null) {
                              value = '';
                              options = $.extend({}, options);
                              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();
                    }
                    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 {
                    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]);
                                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                                  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                                  break;
                                        }
                              }
                    }
                    return cookieValue;
          }
};


調用方法!

$(document).ready(function() {
$('#wCookies').click(function(){
     $.cookie('name', 'test',{expires: 7});                        
});
$('#rCookies').click(function(){
     var test = $.cookie('name');
     alert (test);
});
$('#dCookies').click(function(){
     $.cookie('name', null);                                       
});
});
 

 

 

刪除cookie的一點心得

今天在做購物車時,用到了COOKIE儲存資料,於是是我找了JQUERY的COOKIE操作類,加入我的項目中去。操作還是挺簡單的,根據指令檔上方的介紹就可以用了,雖然是英文,簡單一些的還是可以看明白的。
很快我就在FIREFOX下面做好了功能。可是我在IE瀏覽器中,遇到了一個大麻煩,用了我一個下午都沒有搞定。什麼麻煩呢?COOKIE我是加進去了,也分析出來,顯示出來了。可是當我要刪除時,就是刪除不掉,用法是根據作者的提示做的,應當是對的,可是就是不能刪除。看了一遍又一遍,還是不對,找了網上的資料,也做不對,這時,我就懷疑是不是源碼錯了,搜尋了網上,沒有一個人認為這個源碼是錯的,肯定是自己做錯了嘛。可是又找不到原因,咋辦呢,只好又找了另外一份操作COOKIE的包下來,還是不對。我只好在調試環境下,一遍一遍的測試,找到刪除COOKIE的正確方法。終於讓我找到原因了,原來是PATH,我沒有注意到,所以刪除不掉,儲存COOKIE是這樣寫的  


[jscript]
01.$.cookie("products", cookieContent, { expires: 1 });   
$.cookie("products", cookieContent, { expires: 1 });  刪除COOKIE是這樣寫的


[jscript] 
01.$.cookie("products", null); 
$.cookie("products", null);這樣做是不對的,添加是可以添加,刪除就是刪除不掉,後來改成這樣就可以了
儲存是這樣的


[jscript] 
01.$.cookie("products", cookieContent, { expires: 1, path: '/' }); 
$.cookie("products", cookieContent, { expires: 1, path: '/' });刪除是這樣的


[jscript]  
01.$.cookie("products", null, { path: '/' }); 
$.cookie("products", null, { path: '/' });區別就是要把path賦上去,不然,就是刪除不掉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.