jQuery-cookie用法

安装

在jQuery库之后包含脚本(除非你以其他方式打包脚本):

< script  src = “ /path/to/jquery.cookie.js ” > </ script >
创建会话cookie:

$.cookie(' name '' value ');
7天之后创建过期的Cookie

$.cookie(' name '' value ',{expires : 7 });
创建过期Cookie,在整个网站上有效:

$.cookie(' name '' value ',{expires : 7,path : ' / ' });
阅读Cookie$.cookie(' name '); // =>“value” 
$.cookie(' nothing '); // => undefined
阅读所有可用的Cookie$.cookie(); // => {“name”:“value”}
删除Cookie:

//当cookie被成功删除时返回true,否则为false 
$.removeCookie( ' name '); // => true 
$.removeCookie( ' nothing '); // => false

//需要使用与
$ 一起编写的cookie所使用的相同属性(路径,域)。cookie( ' name '' value ',{path: ' / ' });
//这不行!
$.removeCookie( ' name '); // => false 
//这将工作!
$.removeCookie( ' name ',{path: ' / ' }); // => true
注意:删除Cookie时,您必须传递用于设置Cookie的完全相同的路径,域和安全选项,除非您依赖于默认选项。
用法
==========================================================================
设置cookie
    var date=new Date();
    date.setTime(date.getTime()+(30 * 60 *1000));
    //创建cookie,设置过期时间30分钟,设置路径(保证一个IP下只有一个cookie)
    $.cookie("uuid",data.data.uuid, { expires: date, path:'/' });
            //"cookie名","cookie值", { 有效期: date, 路径:'/'}
    //取cookie
    var uuid=$.cookie("uuid");

    if(uuid){
        //uuid不为空时,执行...
    }

用户操作页面时,重设cookie 30分钟过期时间
    var date= new Date();
    date.setTime(date.getTime()+(30* 60 *1000));
    $.cookie("uuid",uuid, { expires: date, path:'/' });
    var uuid=$.cookie('uuid');

    if(uuid){
        //
    }

删除cookie
    $.cookie("uuid",null,{path:"/"});  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值