cookie函数
function getcookie(){
var cookie={};
var all=document.cookie;
if(all===""){ alert(2);
return cookie;
}
var list=all.split("; ");
for(var i=0;i<list.length;i++){
var cookie=list[i];
var p=cookie.indexOf("="); //返回位置
var name=cookie.substring(0,p);
var value=cookie.substring(p+1);
cookie[name]=value;
console.dir(cookie);
} alert(1);
return cookie;
}
getcookie();
/**
* 设置cookie
* @param {String} name
* @param {String} value
* @param {String} expires
* @param {String} path
* @param {String} domain
* @param {String} secure
*/
function $setCookie(name, value, expires, path, domain, secure) {
var exp = new Date(), expires = arguments[2] || null, path = arguments[3] || "/", domain = arguments[4] || null, secure = arguments[5] || false;
expires ? exp.setMinutes(exp.getMinutes() + parseInt(expires)) : "";
document.cookie = name + '=' + escape(value) + ( expires ? ';expires=' + exp.toGMTString() : '') + ( path ? ';path=' + path : '') + ( domain ? ';domain=' + domain : '') + ( secure ? ';secure' : '');
}
/* --------------------------------------------------
* 获取cookie
* @param {string} name
* @return null 没有找到
* @return ""/string value
*/
function $getCookie(name) {
var reg = new RegExp("(^| |(?=;))" + name + "(?:=([^;]*))?(;|$)"), val = document.cookie.match(reg);
return val ? (val[2] ? unescape(val[2]) : "") : null;
}
/*
* 删除cookie
* @param {String} name
* @param {String} path
* @param {String} domain
* @param {String} secure
*/
function $delCookie(name, path, domain, secure) {
var value = $getCookie(name);
if (value != null) {
var exp = new Date();
exp.setMinutes(exp.getMinutes() - 1000);
path = path || "/";
document.cookie = name + '=;expires=' + exp.toGMTString() + ( path ? ';path=' + path : '') + ( domain ? ';domain=' + domain : '') + ( secure ? ';secure' : '');
}
}
posted on 2013-09-24 11:38 rainbow661314 阅读(166) 评论(0) 编辑 收藏 举报