|
/** |
|
* @authors nuanfeng |
|
* @date 2014-06-09 09:45:46 |
|
* @version v1.0 |
|
*/ |
|
|
|
(function ($) { |
|
$.xcookie = { |
|
cookie: function (name, value, options) { |
|
var path = options.path ? '; path=' + (options.path) : '', |
|
domain = options.domain ? '; domain=' + (options.domain) : '', |
|
secure = options.secure ? '; secure' : '', |
|
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(); |
|
|
|
} |
|
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); |
|
}, |
|
setCookie: function (name, value, options) { |
|
var options = options || {}; |
|
this.cookie(name, value, options); |
|
}, |
|
getCookie: function (name) { |
|
var cookieValue = null, |
|
doc = document; |
|
if (doc.cookie && doc.cookie != '') { |
|
var cookies = doc.cookie.split(';'); |
|
for (var i = 0; i < cookies.length; i++) { |
|
var cookie = $.trim(cookies[i]); |
|
if (cookie.substring(0, name.length + 1) == (name + '=')) { |
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); |
|
break; |
|
} |
|
} |
|
} |
|
return cookieValue; |
|
}, |
|
deleteCookie: function (name, options) { |
|
var options = options || {}, |
|
value = ''; |
|
options.expires = -1; |
|
this.cookie(name, value, options); |
|
} |
|
} |
|
})(jQuery); |
|
|
|
|
|
//使用方法 |
|
// 1、设置 |
|
//$.xcookie.setCookie('gemeOld', ‘cookie值’, { path: '/',expires:1000});//expires是天数 |
|
|
|
//2、获取 |
|
//$.xcookie.getCookie('gemeOld') |
|
|
|
//3、删除 |
|
//$.xcookie.deleteCookie('gemeOld',{path : '/'}) |