jQuery cookie插件详细用法

插件的源代码如下:

jQuery cookie是个很好的cookie插件,大约的运用办法如下
 
 
$.cookie(’name’, ‘value’); 
安装cookie的值,把name变量的值设为value
 
 
$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’,
secure: true}); 
新建一个cookie 富含保质期 路线 域名等
 
$.cookie(’name’, ‘value’); 
 
 
新建cookie
 
$.cookie(’name’, null); 
 
欧洲杯买球下注盘口 , 
去除一个cookie www.2cto.com
 
var account= $.cookie(“name’); 
 
 
取一个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; 
    } 
}; 
 
 
 
第零上空版权全数

复制代码 代码如下:

cookie是个很好的cookie插件,大致的行使情势如下 $.cookie(name, value);
设置cookie的值,把name变量的值设为value $.cookie(name, value, {expires:
7,…

jQuery.cookie = function(name, value, options) {
    if (typeof value != ‘undefined’) {
        options = options || {};
        if (value === null) {
            value = ”;
            options = $.extend({}, options);
            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();
        }
        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 {
        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]);
                if (cookie.substring(0, name.length + 1) == (name +
‘=’)) {
                    cookieValue =
decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

现实用法如下:
1、设置cookie的值,比如大家要安装变量名称叫userid对应值为123的cookie,代码如下:
$.cookie(‘userid’,’123′);
2、新建八个cookie,并安装cookie的得力期 路线 域名等,代码如下:
$.cookie(‘userid, ‘123’, {expires: 7, path: ‘/’, domain: ‘jquery.com’,
secure: true});
瞩目:要是去掉前面{}的参数,新建后将以暗中同意设置生效。
3、删除cookie,即把对应cookie值置为null,代码如下:
$.cookie(‘userid’, null);
4、读取cookie,如读取变量名称为userid的cookie值,代码如下:
var uId= $.cookie(‘userid’);
如何,用jquery来操作cookie是否很简短,你不要自个儿写太多的js,也不用为那多少个包容性发愁,一切就这么顺遂。

您可能感兴趣的稿子:

  • jQuery中cookie插件用法实例深入分析
  • 迅猛学习jQuery插件
    Cookie插件使用方法
  • jquery插件推荐
    jquery.cookie
  • jQuery的cookie插件完结保存用户登录音信
  • jquery插件怎样使用
    jQuery操作Cookie插件使用介绍
  • 基于JQuery的cookie插件
  • jquery cookie插件代码类
  • jQuery轻松操作cookie的插件实例

相关文章