标签:style blog http color io ar for sp div
/*
* @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);
标签:style blog http color io ar for sp div
原文地址:http://www.cnblogs.com/jununx/p/4006460.html