码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie操作

时间:2014-10-13 17:17:09      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   for   sp   div   

Cookie每个域个数、大小限制

bubuko.com,布布扣

Cookie设置、删除、获取

/*
 * @param  name{string}          设置cookie名
 * @param  value{string}         设置cookie值
 * @param  options{object}       设置其他配置参数
 *
 * options.expires               过期时间(ms)
 * options.path                  路径
 * options.domain                域名
 * options.secure                secure值为true时,在http中无效,在https中有效
 */


function
Cookie(name, value, options){ if(typeof value != ‘undefined‘){ options = options || {}; if(value === null){ options.expires = -1; //过期 } var expires = ‘‘; //存在时间选项 if(options.expires && (typeof options.expires == ‘number‘ || options.expires.toUTCString)){ var date; if(typeof options.expires == ‘number‘){ date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); }else{ date = options.expires; } expires = ‘; expires=‘+date.toUTCString(); } var path = options.path ? ‘; path=‘+options.path : ‘‘; var domain = options.domain ? ‘; domain=‘+options.domain : ‘‘; var secure = options.secure ? ‘; secure‘ : ‘‘; //写入cookie document.cookie = [name, ‘=‘, encodeURIComponent(value), expires, path, domain, secure].join(‘‘); }else{//读取cookie var cookValue = null; if(document.cookie && document.cookie != ‘‘){ var cookie = document.cookie.split(‘;‘); for(var i = 0, len = cookie.length; i < len; i++){ var cookie = cookie[i].replace(/^\s+|\s+$/g, ‘‘); if(cookie.substring(0, name.length + 1) == (name + ‘=‘)){ cookValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookValue; } }
//设置 Cookie("user", "小七"); //获取 Cookie("user"); //删除 Cookie("user", null);

 

Cookie操作

标签:style   blog   http   color   io   ar   for   sp   div   

原文地址:http://www.cnblogs.com/jununx/p/4006460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!