Jquery_操作cookies

时间:2023-03-08 16:49:12

首先引入jquery.cookie.js

jquery.cookie.js下地址:http://plugins.jquery.com/cookie/

操作文档:

https://github.com/carhartl/jquery-cookie#readme

创建cookies:

$.cookie('name', 'value');

设置有效期:

设置七天过期
$.cookie('name', 'value', { expires: 7 });
设置cookie一小时后过期
var cookietime = new Date();
cookietime.setTime(date.getTime() + (60 * 60 * 1000));//coockie保存一小时
$.cookie("example", "foo",{expires:cookietime});

设置路径:

$.cookie('name', 'value', { expires: 7, path: '/' });

读取cookies:

$.cookie('name'); // => "value"
$.cookie('nothing'); // => undefined

读取所有的cookies:

$.cookie(); // => { "name": "value" }

删除cookies:

// Returns true when cookie was successfully deleted, otherwise false
$.removeCookie('name'); // => true
$.removeCookie('nothing'); // => false // Need to use the same attributes (path, domain) as what the cookie was written with
$.cookie('name', 'value', { path: '/' });
// This won't work!
$.removeCookie('name'); // => false
// This will work!
$.removeCookie('name', { path: '/' }); // => true

ps:获取最新在线jquery引用地址:http://code.jquery.com/jquery-latest.js