js cookie 工具
var CookieUtil = {
get: function(name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = null; if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(";", cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
}
cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); }
return cookieValue;
},
set: function(name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); if (expires instanceof Date) {
cookieText += "; expires=" + expires.toDateString(); //toGMTString
} if (path) {
cookieText += "; path=" + path;
} if (domain) {
cookieText += "; domain=" + domain;
} if (secure) {
cookieText += "; secure";
} document.cookie = cookieText;
},
unset: function(name, path, domain, secure) {
this.set(name, "", new Date(0), path, domain, secure);
}
};
子cookie
//name=name1=value&name2=value&name3=value&name4=value
//子cookie 的处理方法
var SubCookieUtil = {
get: function (name, subName) {
var subCookies = this.getAll(name);
return subCookies ? subCookies[subName]: null;
}, getAll: function (name) {
var cookieName = encodeURIComponent(name) + "=",
cookieStart = document.cookie.indexOf(cookieName),
cookieValue = null,
result = {}; if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(';', cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
} cookieValue = document.cookie.substring(cookieStart + cookieName.length, cookieEnd); if (cookieValue.length > 0) {
var subCookies = cookieValue.split(' & '); for (var i =0, len = subCookies.length; i < len; i++) {
var parts = subCookies[i].split('=');
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
}
return result;
} return null;
}
}, set: function (name, subName, value, expires, path, domain, secure) {
var subcookies = this.getAll(name) || {};
subcookies[subName] = value;
this.setAll(name, subcookies, expires, path, domain, secure);
},
setAll: function (name, subcookies, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + '=';
var subCookieParts = new Array(); for (var subName in subcookies) {
if (subName.length > 0 && subcookies.hasOwnProperty(subName)) {
subCookieParts.push(encodeURIComponent(subName) + '=' + encodeURIComponent(subcookies[subName]));
}
} if (subCookieParts.length > 0) {
cookieText += subCookieParts.join(" & "); if (expires instanceof Date) {
cookieText += '; expires=' + expires.toDateString();
} if (path) {
cookieText += '; path=' + path;
} if (domain) {
cookieText += '; domain' + domain;
} if (secure) {
cookieText += '; secure';
}
} else {
cookieText += '; expires=' + (new Date(0).toDateString());
} document.cookie = cookieText;
}, unset: function (name, subName, path, domain, secure) {
var subcookies = this.getAll(name);
if (subcookies) {
delete subcookies[subName];
this.setAll(name, subcookies, null, path, domain, secure);
}
},
unsetAll: function (name, path, domain, secure) {
this.setAll(name, null, new Date(0), path, domain, secure);
}
};
来自:《JavaScript 高级程序设计第二版》
js cookie 工具的更多相关文章
- js cookie 工具类
/*cookie start*/ var Cookie=new function(){ //添加cookie this.add=function(name,value,hours){ var life ...
- [JS]Cookie精通之路
[JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- java cookie 工具类
package com.xxx.xxx.xxx.xxx; import java.net.URLDecoder; import java.net.URLEncoder; import javax.se ...
- 2015年最佳的15个 Node.js 开发工具
Node.js 越来月流行,这个基于 Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些工具 ...
- cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题
最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...
- 推荐近期15个 Node.js 开发工具
近来Node.js 越来月流行了,这个基于Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些 ...
- Cookie工具类
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...
- atitit js 开发工具 ide的代码结构显示(func list) outline总结
atitit js 开发工具 ide的代码结构显示(func list) outline总结 eclips环境::4.3.1 #-------需要一个js开发工具,可以显示outline或者代码结构显 ...
随机推荐
- 第二节,mnist手写字体识别
1.获取mnist数据集,得到正确的数据格式 mnist = input_data.read_data_sets('MNIST_data',one_hot=True) 2.定义网络大小:图片的大小是2 ...
- Selenium: Trying to log in with cookies and get the errorMessage - “Can only set cookies for current domain” or "Unable to set Cookie"
from selenium import webdriver driver = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomj ...
- Linux 下的dd命令使用详解(摘录)【转】
转自:https://www.cnblogs.com/jikexianfeng/p/6103500.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意 ...
- cosmic_download-AsyncPool待修正
# !/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/11/16 10:02 AM # @Author : cxa # @File ...
- Linux中error while loading shared libraries错误解决办法
默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件,通常通过源码包进行安装时,如果不指定--prefix,会将库安装在/usr/local/lib目录下:当运行程序需要链接动态库 ...
- hibernate框架学习之数据查询(QBC)helloworld
package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Criteria; import org.hib ...
- $Django importlib与dir知识,手写配置文件, 配置查找顺序 drf分页器&drf版本控制
1 importlib与dir知识 # importlib简介动态导入字符串模块 # 常规导入 from ss.aa import b from ss import a print(b,type(b ...
- Light OJ 1011
题意: (好难看) 给你 N 个 男的, 女的, 男的选女票, 题目给出矩阵, Mp[i][j] 表示 第 i 个男的选 第 J 个女的优先值 选了 J 之后的就不能选 J 了: 求所有狗男女的最大优 ...
- python学习第40天
# 多表查询 # 索引的基础理论 + 数据准备
- Laravel 5.2错误-----Base table or view not found: 1146 Table
报出这个问题,我想就是数据库表名不对导致的. 为什么呢?感觉laravel太强大了,很专业的感觉. 因为它对单词命名的单复数区分的很清楚吧.laravel会自动的将模型名自动替换成名称的复数形式,然后 ...