jquery.cookie使用方法

來源:互聯網
上載者:User

jquery.cookie官網地址

jQuery操作cookie的外掛程式,大概的使用方法如下

//使用方法如下://設定cookie的名值對//$.cookie(’name’, ‘value’);//設定cookie的名值對,有效期間,路徑,域,安全//$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});//建立一個cookie 包括有效期間 路徑 網域名稱等//讀取cookie的值//var account= $.cookie(‘name’);//刪除一個cookie//example $.cookie(’name’, null);  jQuery.cookie = function(name, value, options) {  02     if (typeof value != 'undefined') { // name and value given, set cookie  03         options = options || {};  04         if (value === null) {  05             value = '';  06             options.expires = -1;  07         }  08         var expires = '';  09         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  10             var date;  11             if (typeof options.expires == 'number') {  12                 date = new Date();  13                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  14             } else {  15                 date = options.expires;  16             }  17             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  18         }  19         // CAUTION: Needed to parenthesize options.path and options.domain  20         // in the following expressions, otherwise they evaluate to undefined  21         // in the packed version for some reason...  22         var path = options.path ? '; path=' + (options.path) : '';  23         var domain = options.domain ? '; domain=' + (options.domain) : '';  24         var secure = options.secure ? '; secure' : '';  25         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  26     } else { // only name given, get cookie  27         var cookieValue = null;  28         if (document.cookie && document.cookie != '') {  29             var cookies = document.cookie.split(';');  30             for (var i = 0; i < cookies.length; i++) {  31                 var cookie = jQuery.trim(cookies[i]);  32                 // Does this cookie string begin with the name we want?  33                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  34                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  35                     break;  36                 }  37             }  38         }  39         return cookieValue;  40     }  41 }; 

 

這是掌握cookie最後的一個障礙:預設情況下cookie只能被在
同一個Web伺服器上同一個路徑下設定了該cookie的網頁讀取.
例如,如果在
"http://kenny.safeter.com/food/kenny/banana_kenny.htm"
有一段Javascript詢問了使用者的姓名,你可能需要在你的另一
個網頁例如首頁中訪問一個給定的名字.所以你必須設定該
cookie的路徑.路徑"path"用於設定可以讀取一個cookie的最
頂層的目錄.將cookie的路徑設定為你的網頁最頂層的目錄可
以讓該該目錄下的所有網頁都能訪問該cookie.

方法:在你的cookie中加入path=/; 如果你只想讓"food" 目錄
中的網頁可以使用該cookie,則你加入path=/food;.還有一點:
有些網站有許多小的網域名稱,例如網猴可能還在
"chimp.webmonkey.com," "gorilla.webmonkey.com," 和
"ape.webmonkey.com." 網域名稱下有網頁.預設情況下只有
"chimp.webmonkey.com" 域下的網頁可以讀取該cookie.如果
你向讓"webmonkey.com"下的所有機器都可以讀取該cookie,我
們必須在cookie中加入 "domain=webmonkey.com" .

要將一個cookie設定在
"http://chimp.webmonkey.com/food/bananas/banana_puree.htm"
並且讓所有網猴的網頁都可以利用它,我們可以這樣:

代碼

function setCookie(){    var the_name = prompt("What's your name?","");    var the_cookie ="cookie_puss=" + escape(the_name) + ";" ;    var the_cookie = the_cookie+ "path=/;";    var the_cookie = the_cookie + "domain=webmonkey.com;";    document.cookie =the_cookie;}

 

聯繫我們

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