jquery操作cookie

网站建设 2023-01-28 20:48www.1681989.com免费网站

复制代码
代码如下:
jQuery.cookie = function(name, value, options) { if (typeof value != 'undefed') { // 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.toUTCStrg)) { 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.toUTCStrg(); // use expires attribute, max-age is not supported by IE } var path = options.path ? '; path=' + options.path : ''; var doma = options.doma ? '; doma=' + options.doma : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, doma, secure].jo(''); } 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 strg beg with the name we want? if (cookie.substrg(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substrg(name.length + 1)); break; } } } return cookieValue; } }; function getcookie(name) { var cookie_start = document.cookie.dexOf(name); var cookie_end = document.cookie.dexOf(";", cookie_start); return cookie_start == -1 ? '' : unescape(document.cookie.substrg(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length))); } function setcookie(cookieName, cookieValue, seconds, path, doma, secure) { var expires = new Date(); expires.setTime(expires.getTime() + seconds); document.cookie = escape(cookieName) + '=' + escape(cookieValue) + (expires ? '; expires=' + expires.toGMTStrg() : '') + (path ? '; path=' + path : '/') + (doma ? '; doma=' + doma : '') + (secure ? '; secure' : ''); }
使用方法 提供方便方法操作cookie :
复制代码
代码如下:
$.cookie('the_cookie'); // 获得cookie $.cookie('the_cookie', 'the_value'); // 设置cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie 7天 $.cookie('the_cookie', '', { expires: -1 }); // 删除 $.cookie('the_cookie', null); // 删除 cookie 设置cookie的名值对,有效期,路径,域,安全 $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, doma: ‘jquery.’, secure: true});

Copyright © 2016-2025 www.1681989.com 推火网 版权所有 Power by