例如購物網(wǎng)站存儲(chǔ)用戶曾經(jīng)瀏覽過的產(chǎn)品列表,或者門戶網(wǎng)站記住用戶喜歡選擇瀏覽哪類新聞。 在用戶允許的情況下,還可以存儲(chǔ)用戶的登錄信息,使得用戶在訪問網(wǎng)站時(shí)不必每次都鍵入這些信息?
怎么在js/jquery中操作處理cookie那?今天分享一個(gè)cookie操作類--jQuery.Cookie.js,是一個(gè)輕量級(jí)的Cookie管理插件。
Cookie下載地址: http://plugins.jquery.com/project/cookie.
特別提醒,今日發(fā)現(xiàn)一個(gè)特別的錯(cuò)誤,google瀏覽器提示:has no method $.cookie。火狐瀏覽器提示:$.cookie is not a function;調(diào)試了半天,終于找到原因,如果同一個(gè)頁面兩次或者多次引入Jquery插件就會(huì)報(bào)此錯(cuò)誤。
使用方法:
1、引入jQuery與jQuery.Cookie.js插件。
代碼如下:
創(chuàng)建一個(gè)cookie并設(shè)置有效時(shí)間為7天:
$.cookie('the_cookie', 'the_value', { expires: 7 });
注:當(dāng)指明了cookie有效時(shí)間時(shí),所創(chuàng)建的cookie被稱為“持久cookie(persistent cookie)”。
創(chuàng)建一個(gè)cookie并設(shè)置cookie的有效路徑:
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
注:在默認(rèn)情況下,只有設(shè)置cookie的網(wǎng)頁才能讀取該cookie。如果想讓一個(gè)頁面讀取另一個(gè)頁面設(shè)置的cookie,必須設(shè)置cookie的路徑。
cookie的路徑用于設(shè)置能夠讀取cookie的頂級(jí)目錄。將這個(gè)路徑設(shè)置為網(wǎng)站的根目錄,可以讓所有網(wǎng)頁都能互相讀取cookie(一般不要這樣設(shè)置,防止出現(xiàn)沖突)
讀取cookie:
$.cookie('the_cookie');
// cookie存在 => 'the_value' $.cookie('not_existing'); // cookie不存在 => null
刪除cookie,通過傳遞null作為cookie的值即可:
$.cookie('the_cookie', null);
相關(guān)參數(shù)的解釋
expires: 365
定義cookie的有效時(shí)間,值可以是一個(gè)(從創(chuàng)建cookie時(shí)算起,以天為單位)或一個(gè)Date。
如果省略,那么創(chuàng)建的cookie是會(huì)話cookie,將在用戶退出瀏覽器時(shí)被刪除。
path: '/'
默認(rèn)情況:只有設(shè)置cookie的網(wǎng)頁才能讀取該cookie。
定義cookie的有效路徑。默認(rèn)情況下,該參數(shù)的值為創(chuàng)建cookie的網(wǎng)頁所在路徑(標(biāo)準(zhǔn)瀏覽器的行為)。
如果你想在整個(gè)網(wǎng)站中訪問這個(gè)cookie需要這樣設(shè)置有效路徑:path: '/'。
如果你想刪除一個(gè)定義了有效路徑的cookie,你需要在調(diào)用函數(shù)時(shí)包含這個(gè)路徑:$.cookie('the_cookie', null, { path: '/' });。
domain: 'example.com'
默認(rèn)值:創(chuàng)建cookie的網(wǎng)頁所擁有的域名。
secure: true
默認(rèn)值:false。如果為true,cookie的傳輸需要使用安全協(xié)議(HTTPS)。
raw: true
默認(rèn)值:false。 默認(rèn)情況下,讀取和寫入cookie的時(shí)候自動(dòng)進(jìn)行編碼和解碼(使用encodeURIComponent編碼,decodeURIComponent解碼)。
要關(guān)閉這個(gè)功能設(shè)置raw: true即可。
$.cookie('the_cookie'); // get cookie $.cookie('the_cookie', 'the_value'); // set cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future $.cookie('the_cookie', '', { expires: -1 }); // delete cookie
$.cookie('the_cookie', null); // delete cookie
$.cookie('the_cookie','the_value', {expires: 7, path: '/', domain:'80tvb.com', secure: true});//完整調(diào)用方式
//或者這樣:$.cookie('the_cookie','the_value');
//刪除Cookie: $.cookie('the_cookie',null);
jQuery操作cookie的插件,大概的使用方法如下
$.cookie('the_cookie'); //讀取Cookie值
$.cookie('the_cookie', ‘the_value'); //設(shè)置cookie的值
$.cookie('the_cookie', ‘the_value', {expires: 7, path: ‘/', domain: ‘jquery.com', secure: true});//新建一個(gè)cookie 包括有效期 路徑域名等
$.cookie('the_cookie', ‘the_value'); //新建cookie
$.cookie('the_cookie', null); //刪除一個(gè)cookie
jquery設(shè)置cookie過期時(shí)間與檢查cookies是否可用
讓cookies在x分鐘后過期
var date = new date();
date.settime(date.gettime() + (x * 60 * 1000));
$.cookie(‘example', ‘foo', { expires: date });
$.cookie(‘example', ‘foo', { expires: 7});
檢查cookies是否可用
$(document).ready(function() {var dt = new date();dt.setseconds(dt.getseconds() + 60);document.cookie = “cookietest=1; expires=” + dt.togmtstring();var cookiesenabled = document.cookie.indexof(“cookietest=”) != -1;if(!cookiesenabled){//cookies不能用……..}});
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com