jquery插件如何使用 jQuery操作Cookie插件使用介紹
更新時間:2012年12月15日 16:31:52 作者:
本文將介紹jQuery如何操作Cookie插件,需要了解的朋友可以參考下
代碼:
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
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
}
// NOTE Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
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;
}
};
使用方法
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});//新建一個cookie 包括有效期 路徑 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //刪除一個cookie
設(shè)置一個名稱為blog,值為css9.net的cookie:
$.cookie("blog", "css9.net");
設(shè)置一個名稱為blog,值為css9.net的cookie,同時設(shè)置過期時間(expires屬性)為7天:
$.cookie("blog", "css9.net", { expires: 7 });
設(shè)置一個名稱為blog,值為css9.net的cookie,設(shè)置過期時間(expires屬性)為7天,同時設(shè)置cookie的path屬性為”/admin”
$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });
讀取Cookie:
讀取名稱為blog的cookie值:
alert( $.cookie("blog") );
刪除cookie:
$.cookie("example", null);
復(fù)制代碼 代碼如下:
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
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
}
// NOTE Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
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;
}
};
使用方法
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});//新建一個cookie 包括有效期 路徑 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //刪除一個cookie
設(shè)置一個名稱為blog,值為css9.net的cookie:
$.cookie("blog", "css9.net");
設(shè)置一個名稱為blog,值為css9.net的cookie,同時設(shè)置過期時間(expires屬性)為7天:
$.cookie("blog", "css9.net", { expires: 7 });
設(shè)置一個名稱為blog,值為css9.net的cookie,設(shè)置過期時間(expires屬性)為7天,同時設(shè)置cookie的path屬性為”/admin”
$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });
讀取Cookie:
讀取名稱為blog的cookie值:
alert( $.cookie("blog") );
刪除cookie:
$.cookie("example", null);
相關(guān)文章
jQuery實現(xiàn)鼠標(biāo)雙擊Table單元格變成文本框及輸入內(nèi)容后更新到數(shù)據(jù)庫的方法
這篇文章主要介紹了jQuery實現(xiàn)鼠標(biāo)雙擊Table單元格變成文本框及輸入內(nèi)容后更新到數(shù)據(jù)庫的方法,涉及jQuery響應(yīng)鼠標(biāo)事件動態(tài)操作頁面元素及基于get實現(xiàn)ajax交互保存數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-11-11Jquery選擇子控件"大于號"和" "區(qū)別介紹及使用示例
Jquery選擇子控件”>“:在給定的父元素下匹配所有的子元素;另一個就是在給定的祖先元素下匹配所有的后代元素,具體概述及使用示例如下,感興趣的朋友可以參考下哈2013-06-06jQuery常用事件方法mouseenter+mouseleave+hover
這篇文章主要介紹了jQuery常用事件方法mouseenter、mouseleave和hover方法,下文內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下2022-03-03