Cookie plug-in of jQuery
02 |
jQuery.cookie = function(name, value, options) { |
03 |
if (typeof value != 'undefined') { |
04 |
options = options || {}; |
07 |
options = $.extend({}, options); |
11 |
if (options.expires && (typeof options.expires == 'number'|| options.expires.toUTCString)) { |
13 |
if (typeof options.expires == 'number') { |
15 |
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); |
17 |
date = options.expires; |
19 |
expires = '; expires=' + date.toUTCString(); |
21 |
var path = options.path ? '; path=' + (options.path) : ''; |
22 |
var domain = options.domain ? '; domain=' + (options.domain) : ''; |
23 |
var secure = options.secure ? '; secure' : ''; |
24 |
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); |
26 |
var cookieValue = null; |
27 |
if (document.cookie && document.cookie != '') { |
28 |
var cookies = document.cookie.split(';'); |
29 |
for (var i = 0; i < cookies.length; i++) { |
30 |
var cookie = jQuery.trim(cookies[i]); |
31 |
if (cookie.substring(0, name.length + 1) == (name + '=')) { |
32 |
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); |
The usage is as follows:
1. Set the cookie value. For example, to set the cookie with the variable userid 123, the Code is as follows:
$. Cookie ('userid', '20140901 ');
2. Create a cookie and set the Domain Name of the cookie's validity period. The Code is as follows:
$. Cookie ('userid, '000000', {expires: 7, path: '/', domain: 'jquery. com', secure: true });
Note: If the {} parameter is removed, it will take effect with the default setting after the new parameter is created.
3. delete a cookie. Set the cookie value to null. The Code is as follows:
$. Cookie ('userid', null );
4. Read the cookie. For example, to read the cookie value of the variable named userid, the Code is as follows:
Var uId = $. cookie ('userid ');