js中的cookie操作
一、js cookie 使用时把此段代码引入页面
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
var _OldCookies = window.Cookies;
var api = window.Cookies = factory(window.jQuery);
api.noConflict = function () {
window.Cookies = _OldCookies;
return api;
};
}
}(function () {
function extend () {
var i = 0;
var result = {};
for (; i < arguments.length; i++) {
var attributes = arguments[ i ];
for (var key in attributes) {
result[key] = attributes[key];
}
}
return result;
} function init (converter) {
function api (key, value, attributes) {
var result; // Write if (arguments.length > 1) {
attributes = extend({
path: '/'
}, api.defaults, attributes); if (typeof attributes.expires === 'number') {
var expires = new Date();
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
attributes.expires = expires;
} try {
result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {} value = encodeURIComponent(String(value));
value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); key = encodeURIComponent(String(key));
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
key = key.replace(/[\(\)]/g, escape); return (document.cookie = [
key, '=', value,
attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
attributes.path && '; path=' + attributes.path,
attributes.domain && '; domain=' + attributes.domain,
attributes.secure ? '; secure' : ''
].join(''));
} // Read if (!key) {
result = {};
} // To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling "get()"
var cookies = document.cookie ? document.cookie.split('; ') : [];
var rdecode = /(%[0-9A-Z]{2})+/g;
var i = 0; for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var name = parts[0].replace(rdecode, decodeURIComponent);
var cookie = parts.slice(1).join('='); if (cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
} try {
cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent); if (this.json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
} if (key === name) {
result = cookie;
break;
} if (!key) {
result[name] = cookie;
}
} catch (e) {}
} return result;
} api.get = api.set = api;
api.getJSON = function () {
return api.apply({
json: true
}, [].slice.call(arguments));
};
api.defaults = {}; api.remove = function (key, attributes) {
api(key, '', extend(attributes, {
expires: -1
}));
}; api.withConverter = init; return api;
} return init();
}));
设置cookie
Cookies(name, value); || Cookies.set(name, value); //name:所存cookie的名称;value:名称对应的值
Cookies(name1, value1); || Cookies.set(name1, value1);
Cookies(name,value, { expires: 7 }); || Cookies.set(name,value, { expires: 7 }); // 设置cookie失效时间,单位:天
Cookies(name,value, { expires: 7,path:"/路径",domain: "域名"}); || Cookies.set(name,value, { expires: 7,path:"/路径",domain: "域名"});// 设置cookie,包括有效期 路径 域名等
读取cookie
Cookies(); || Cookies.get(); //读取所有cookie,输出的是对象==>{name:value, name1:value1}
Cookies(name); || Cookies.get(name); //读取对应名称的cookie==>value
删除cookie
Cookies(name:""); //将cookie清空
Cookie.remove(name:""); //删除cookie 对应的cookie名称也删除
js中的cookie操作的更多相关文章
- 【原创】js中利用cookie实现记住密码功能
在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...
- js中的DOM操作汇总
一.DOM创建 DOM节点(Node)通常对应于一个标签,一个文本,或者一个HTML属性.DOM节点有一个nodeType属性用来表示当前元素的类型,它是一个整数: Element,元素 Attrib ...
- js中的json操作
js中的json操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScr ...
- js中对cookie的操作及json数据与cookie结合的用法
cookie的使用 添加cookie 添加cookie:document.cookie = “key=value”; // 一次写入一个键值对 document.cookie = 'test1=hel ...
- 在jsp页面的js中使用Cookie的原理介绍以及相应方法的代码
1. 设置cookie 1.1 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie: document.cookie="user_Id=828&q ...
- js中常用的操作
1.js中常用的数组操作 2.js中常用的字符串操作 3.js中常用的时间日期操作 4.定时器
- 在node.js中使用COOKIE
node.js中如何向客户端发送COOKIE呢?有如下两个方案: 一.使用response.writeHead,代码示例: //设置过期时间为一分钟 var today = new Date(); v ...
- js 中的cookie
根据智能社31cookie基础与应用总结, cookie的特性: 1.同一个网站,共用一套cookie,实际上是根据域名来区分的. 如t.sina.com.cn ,和weibo.com这两个都是新浪微 ...
- JS中获取和操作iframe
一.需求与遇到的问题 在网站的后台管理中使用了iframe框架布局,包括顶部菜单.左侧导航和主页面.需求是:点击主页面上的一个按钮,在顶部菜单栏的右侧显示“退出”链接,点击可退出系统. 我的思路是:在 ...
随机推荐
- 决策树Decision Tree 及实现
Decision Tree 及实现 标签: 决策树熵信息增益分类有监督 2014-03-17 12:12 15010人阅读 评论(41) 收藏 举报 分类: Data Mining(25) Pyt ...
- tp生成验证码
视图层: <input type="text" name="code" value="" /> <img o ...
- 1.2 如何在visual studio 中建立C#程序
这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大 ...
- 深入理解Android之Gradle
深入理解Android之Gradle 格式更加精美的PDF版请到:http://vdisk.weibo.com/s/z68f8l0xTYrZt 下载 Gradle是当前非常"劲爆" ...
- 在linux中的info手册的用法
就是一些快捷键 空格键向下翻页,当处在当前节点的底部时,空格键跳转到下一个节点. <DEL> 或者 <BACKSPACE> 向上翻页,当处在当前节点的顶部的时候,这两个键可以跳 ...
- 企业信息系统——CRM
客户关系管理系统(Customer Relationship Management)将客户看作是企业的一项重要资产,客户关怀是CRM的中心,其目的是与客户建立长期和有效的业务关系,在与客户的每个“接触 ...
- Xamarin
快速建立原生(Native)的行动装置应用程序: 程序代码共享: 与 Visual Studio 整合: 确保第一时间更新: 原生的应用程序效能:
- 【request_firmware】 linux内核下载模块固件接口【转】
转自:http://blog.csdn.net/magod/article/details/6049558 [-] 8 处理固件 1481 内核固件接口 1482 它如何工作 14.8. 处理固件 作 ...
- Frost R&D
Trees Procedural Math Model in Houdini,render with Mantra. Shader use SurfaceModel With Other Attrib ...
- [课程设计]Scrum 1.7 多鱼点餐系统开发进度(点餐菜式内容添加及美化)
[课程设计]Scrum 1.7 多鱼点餐系统开发进度(点餐菜式内容添加及美化) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...