Instantly share code, notes, and snippets.
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save monjer/5187755 to your computer and use it in GitHub Desktop.
cookie操作方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author hmj | |
| * @date 2012.7.30 | |
| */ | |
| (function(opt_ns , opt_wind){ | |
| var g_win = opt_wind || top ; | |
| var g_doc = g_win.document ; | |
| function rawStr(str){ | |
| return str ; | |
| }; | |
| var cookie = { | |
| /** | |
| * @purpose 验证cookie的value的合法性 | |
| * @param {String} value value字符串 | |
| * @private | |
| */ | |
| _validateCookieValue:function(value){ | |
| if(typeof value === "undefined"){ | |
| this._error("cookie's value cannot be undefined !") | |
| } | |
| }, | |
| /** | |
| * @purpose 异常处理 | |
| * @param {String} message | |
| * @private | |
| */ | |
| _error : function(message){ | |
| throw new TypeError(message); | |
| }, | |
| /** | |
| * @purpose 验证cookie的name的合法性 | |
| * @param {String} key Cooke name | |
| * @private | |
| */ | |
| _validateCookieNameKey : function(key){ | |
| if(typeof key !== "string" || /^\s*$/.test(key)){ | |
| this._error("cookie's value must be no empry string !"); | |
| } | |
| }, | |
| /** | |
| * @purpose 解析cookie字符串的值,生成cookie对象 | |
| * @param {String} cookieText cookie字符串,来源于document.cookie | |
| * @param {Boolean} decode 是否需要解密 | |
| * @return {Object} 结果对象 | |
| * @private | |
| */ | |
| _parseCookie:function(cookieText , decode){ | |
| var decodeFn = decode ? decodeURIComponent : rawStr ; | |
| var cookie = {} ; | |
| //cookie属性的分隔符"; " | |
| var spliterReg = /;\s/g ; | |
| var cookiePairs = cookieText.split(spliterReg) ; | |
| var length = cookiePairs.length ; | |
| var cookieName = null ; | |
| var cookieValue = null ; | |
| for(var i = 0 ; i < length ; i++){ | |
| //cookie以key=value形式存在 | |
| cookieName = cookiePairs[i].match(/([^=]+)=/); | |
| //合法的k/v值 | |
| if(cookieName instanceof Array){ | |
| cookieName = decodeFn(cookieName[1]); | |
| cookieValue = cookiePairs[i].substring(cookieName.length + 1); | |
| //设置cookie的name-value浏览器并不会检查格式 | |
| }else{ | |
| cookieName = decode(cookiePairs[i]); | |
| cookieValue = ""; | |
| } | |
| cookie[cookieName] = cookieValue ; | |
| } | |
| return cookie ; | |
| }, | |
| /** | |
| * @purpose 创建cookie字符串,格式与Set-Cookie消息头相一致 | |
| * @param {String} key cookie的key | |
| * @param {String} value cookie的value | |
| * @param {Boolean} encodeValue 是否需要转码 | |
| * @param {Object} attr cookie的属性对象 | |
| * @options {Date} expires | |
| * @options {Boolean} secure 安全链接标志 | |
| * @options {Boolean} httponly httponly 标记 | |
| * @options {String} domain cookie的所在域 | |
| * @options {String} path cookie的路径,子路劲下cookie可见 | |
| * @return {String} 新生成的cookie字符串 | |
| * @private | |
| */ | |
| _createCookieText:function(key , value , encodeValue , attr ){ | |
| attr = attr || {} ; | |
| var encodeFn = encodeValue == true ? encodeURIComponent : rawStr ; | |
| if(typeof attr.expires === "number"){ | |
| var days = attr.expires; | |
| var curdate = attr.expires = new Date(); | |
| attr.expires.setDate(curdate.getDate() + days); | |
| } | |
| return [ | |
| key + "=" + encodeFn(value) , | |
| attr.domain ? "; domain="+attr.domain : "" , | |
| attr.path ? "; path="+attr.path : "" , | |
| attr.expires ? "; expires="+attr.expires.toUTCString(): "", | |
| attr.secure ? "; secure" : "" , | |
| attr.httponly ? "; httponly" : "" | |
| ].join("") ; | |
| }, | |
| /** | |
| * @purpose 设置cookie,包括create和update操作 | |
| * @param {String} key cookie的名称 | |
| * @param {String} value cookie的值 | |
| * @param {Object} options cookie的属性 | |
| * @options {Boolean} raw 是否需要对键值转码 | |
| * @options {Date} expires 失效日期 GMT 格式 | |
| * @options {Boolean} secure 安全链接标志 | |
| * @options {Boolean} httponly httponly 标记 | |
| * @options {String} domain cookie的所在域 | |
| * @options {String} path cookie的路径,子路径下cookie可见 | |
| * @return {String} 新生成的cookie字符串 | |
| * @public | |
| */ | |
| set:function(key , value , options){ | |
| this._validateCookieNameKey(key); | |
| this._validateCookieValue(value); | |
| options = options || {} ; | |
| var cookieString = this._createCookieText(key , value , !options.raw , options); | |
| // 直接进行赋值操作,新的cookie文本会直接追加至document.cookie中 | |
| g_doc.cookie = cookieString ; | |
| return cookieString ; | |
| }, | |
| /** | |
| * @purpose 获取cookie的值 | |
| * @param {String} key cookie的名称 | |
| * @return {String} key所对应的value | |
| * @public | |
| */ | |
| get:function(key){ | |
| this._validateCookieNameKey(key); | |
| var cookie = this._parseCookie(g_doc.cookie); | |
| if(cookie.hasOwnProperty(key)){ | |
| return cookie[key] ; | |
| } | |
| return "" ; | |
| }, | |
| /** | |
| * @purpose 判断当前cookie中是否存在指定的key | |
| * @param {String} key cookie的名称 | |
| * @return {Boolean} | |
| * @public | |
| */ | |
| has:function(key){ | |
| this._validateCookieNameKey(key); | |
| var cookie = this._parseCookie(g_doc.cookie); | |
| if(cookie.hasOwnProperty(key)){ | |
| return true ; | |
| } | |
| return false ; | |
| }, | |
| /** | |
| * @purpose 删除cookie中指定的key | |
| * @param {String} options cookie的属性选项 | |
| * @options {Date} expires 失效日期 GMT 格式 | |
| * @options {Boolean} secure 安全链接标志 | |
| * @options {Boolean} httponly httponly 标记 | |
| * @options {String} domain cookie的所在域 | |
| * @options {String} path cookie的路径,子路劲下cookie可见 | |
| * @public | |
| */ | |
| remove:function(key , options){ | |
| this._validateCookieNameKey(key); | |
| var options = options || {} ; | |
| //设置失效期为过去的时间点即可 | |
| options.expires = new Date(0); | |
| return this.set(key , "" , options); | |
| } | |
| } ; | |
| opt_wind = opt_wind || top ; | |
| opt_ns = opt_ns || opt_wind; | |
| opt_ns.cookie = cookie ; | |
| })(undefined , top); |
monjer
commented
Dec 25, 2013
Author
- Cookie wiki en
- Cookie wiki cn
- RFC2019
- Servlet Cookie API
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment