一、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操作的更多相关文章

  1. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

  2. js中的DOM操作汇总

    一.DOM创建 DOM节点(Node)通常对应于一个标签,一个文本,或者一个HTML属性.DOM节点有一个nodeType属性用来表示当前元素的类型,它是一个整数: Element,元素 Attrib ...

  3. js中的json操作

    js中的json操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScr ...

  4. js中对cookie的操作及json数据与cookie结合的用法

    cookie的使用 添加cookie 添加cookie:document.cookie = “key=value”; // 一次写入一个键值对 document.cookie = 'test1=hel ...

  5. 在jsp页面的js中使用Cookie的原理介绍以及相应方法的代码

    1. 设置cookie 1.1 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie: document.cookie="user_Id=828&q ...

  6. js中常用的操作

    1.js中常用的数组操作 2.js中常用的字符串操作 3.js中常用的时间日期操作 4.定时器

  7. 在node.js中使用COOKIE

    node.js中如何向客户端发送COOKIE呢?有如下两个方案: 一.使用response.writeHead,代码示例: //设置过期时间为一分钟 var today = new Date(); v ...

  8. js 中的cookie

    根据智能社31cookie基础与应用总结, cookie的特性: 1.同一个网站,共用一套cookie,实际上是根据域名来区分的. 如t.sina.com.cn ,和weibo.com这两个都是新浪微 ...

  9. JS中获取和操作iframe

    一.需求与遇到的问题 在网站的后台管理中使用了iframe框架布局,包括顶部菜单.左侧导航和主页面.需求是:点击主页面上的一个按钮,在顶部菜单栏的右侧显示“退出”链接,点击可退出系统. 我的思路是:在 ...

随机推荐

  1. Android studio 查看签名

    根据密钥查看 根据安装包查看:改apk为zip 解压 打开 META-INF --->cmd: keytool -printcert -file CERT.RSA

  2. ajax 使用

    写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...

  3. 在linux中访问virtualbox的共享文件夹

    1.在客户机里需要安装Virtualbox的增强功能. 2.使用virtualbox的图形界面设置好共享文件夹. 3.假设你设置的共享文件夹的名称是 share,使用如下命令在客户机的linux系统中 ...

  4. org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 421. occured resuming VM.

    环境: 导入excel的时候,会根据路径,读取EXCEL的数据. 原因: 电脑上的防火墙关闭

  5. web前端本地测试方法

    在大型项目中的前端测试与开发,通常使用git clone将整个工程目录下载下来,然后本地运行调试. 然而,当一个项目已经发布到测试机上,需要临时查看某个页面的效果或修改某个页面时,clone整个工程目 ...

  6. HtmlHelper的扩展

    HtmlHelper的扩展: 注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束. //主要就是输出分页的超级链接 ...

  7. s3c2440 移值新内核 linux-4.1.36

    arm-linuxgcc version 4.3.2 经过试验,最高可以编译到 linux-4.1.36 ,在高的版本会有错误 ,可能是 GCC 编译器版本较低造成. 解压比较麻烦还要装一个 xz x ...

  8. web页面状态

    200 :表示请求成功,服务器已处理 201 :表示服务器端请求被创建, 202 :表示服务器端请求已经被接收,但未处理完成 400 :表示错误的请求 404:表示请求的资源未找到. 405:表示没有 ...

  9. WordPress实现登录或退出后直接跳转到首页的方法

    现在Wordpress是登录之后跳回到我们上次查看的页面,那么要如何修改它登录之后直接跳回到首页呢,这里就来给给大家详细介绍一下解决方法. 首先定位到登录链接所在位置,你会看到类似下面的代码: 复制代 ...

  10. 浙江理工2015.12校赛-G Jug Hard

    Jug Hard Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1172 Solved: 180 Description You have two e ...