您的位置:首页 > Web前端 > JavaScript

[转] Javascript访问Cookie的四个常用方法

2008-03-31 16:48 706 查看
// utility function called by getCookie()

function getCookieVal(offset) {

var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) {

endstr = document.cookie.length;

}

return unescape(document.cookie.substring(offset, endstr));

}
// primary function to retrieve cookie by name

function getCookie(name) {

var arg = name + "=";

var alen = arg.length;

var clen = document.cookie.length;

var i = 0;

while (i < clen) {

var j = i + alen;

if (document.cookie.substring(i, j) == arg) {

return getCookieVal(j);

}

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) break;

}

return null;

}

// store cookie value with optional details as needed

function setCookie(name, value, expires, path, domain, secure) {

document.cookie = name + "=" + escape (value) +

((expires) ? "; expires=" + expires : "") +

((path) ? "; path=" + path : "") +

((domain) ? "; domain=" + domain : "") +

((secure) ? "; secure" : "");

}

// remove the cookie by setting ancient expiration date

function deleteCookie(name,path,domain) {

if (getCookie(name)) {

document.cookie = name + "=" +

((path) ? "; path=" + path : "") +

((domain) ? "; domain=" + domain : "") +

"; expires=Thu, 01-Jan-70 00:00:01 GMT";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: