基于jquery的cookie的用法
設(shè)置cookie的值,把name變量的值設(shè)為value
example $.cookie('name', ‘value', {expires: 7, path: ‘/', domain: ‘jquery.com', secure: true});
新建一個(gè)cookie 包括有效期 路徑 域名等
example $.cookie('name', ‘value');
新建cookie
example $.cookie('name', null);
刪除一個(gè)cookie
var account= $.cookie('name');
取一個(gè)cookie(name)值給myvar
代碼如下
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;
}
};
然后看了下Discuz!中對(duì)cookie的操作方法
如下,發(fā)現(xiàn)少了個(gè)遍歷用;分割的數(shù)組的處理
function getcookie(name) {
var cookie_start = document.cookie.indexOf(name);
var cookie_end = document.cookie.indexOf(";", cookie_start);
return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
var expires = new Date();
expires.setTime(expires.getTime() + seconds);
document.cookie = escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '/')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}
- 通過(guò)Jquery.cookie.js實(shí)現(xiàn)展示瀏覽網(wǎng)頁(yè)的歷史記錄超管用
- jquery使用Cookie和JSON記錄用戶(hù)最近瀏覽歷史
- jquery.cookie() 方法的使用(讀取、寫(xiě)入、刪除)
- jquery.cookie用法詳細(xì)解析
- 使用jQuery操作Cookies的實(shí)現(xiàn)代碼
- jquery.cookie.js 操作cookie實(shí)現(xiàn)記住密碼功能的實(shí)現(xiàn)代碼
- jQuery操作cookie方法實(shí)例教程
- jQuery控制cookie過(guò)期時(shí)間的方法
- jQuery的cookie插件實(shí)現(xiàn)保存用戶(hù)登陸信息
- 怎樣使用php與jquery設(shè)置和讀取cookies
- jquery cookie插件代碼類(lèi)
- jquery插件如何使用 jQuery操作Cookie插件使用介紹
- jQuery.cookie.js實(shí)現(xiàn)記錄最近瀏覽過(guò)的商品功能示例
相關(guān)文章
jQuery zTree插件快速實(shí)現(xiàn)目錄樹(shù)
這篇文章主要為大家詳細(xì)介紹了jQuery zTree插件快速實(shí)現(xiàn)目錄樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08jQuery插件HighCharts繪制簡(jiǎn)單2D柱狀圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件HighCharts繪制簡(jiǎn)單2D柱狀圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用HighCharts插件繪制2D柱狀圖的實(shí)現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03jQuery實(shí)現(xiàn)QQ空間漢字轉(zhuǎn)拼音功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)QQ空間漢字轉(zhuǎn)拼音功能,結(jié)合具體實(shí)例形式分析了jQuery實(shí)現(xiàn)拼音與中文漢字的轉(zhuǎn)換操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07jQuery實(shí)現(xiàn)html雙向綁定功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)html雙向綁定功能,涉及jQuery針對(duì)HTML頁(yè)面元素事件綁定相關(guān)操作技巧,需要的朋友可以參考下2017-10-10實(shí)現(xiàn)easyui的datagrid導(dǎo)出為excel的示例代碼
下面小編就為大家?guī)?lái)一篇實(shí)現(xiàn)easyui的datagrid導(dǎo)出為excel的示例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11如何實(shí)現(xiàn)星星評(píng)價(jià)(jquery.raty.js插件)
本文主要分享了用jQuery插件jquery.raty.js實(shí)現(xiàn)星星評(píng)價(jià)功能:后臺(tái)傳數(shù)據(jù),前臺(tái)顯示星星個(gè)數(shù)的具體方法。有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12基于jQuery實(shí)現(xiàn)掛號(hào)平臺(tái)首頁(yè)源碼
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)掛號(hào)平臺(tái)首頁(yè)源碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01JavaScript前端頁(yè)面搜索功能案例【基于jQuery】
這篇文章主要介紹了JavaScript前端頁(yè)面搜索功能案例,結(jié)合完整實(shí)例形式詳細(xì)分析了基于jQuery實(shí)現(xiàn)的前端頁(yè)面表格搜索功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07