javascript中操作COOKIE的函数[收藏]
一直想明白如何用javascript来控制cookie,比如带记忆功能的显示/隐藏层。
在天空的博客中,无意发现这篇(javascript中操作COOKIE的函数),收藏了。
function Setcookie (name, value) { //设置名称为name,值为value的Cookie
var argc = SetCookie.arguments.length;
var argv = SetCookie.arguments;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + value +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function GetCookie (name) { //取得名称为name的cookie值
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;
}
function Deletecookie (name) { //删除名称为name的Cookie
var exp = new Date(); exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name); document.cookie = name + "=" + cval + ";
expires=" + exp.toGMTString();
}
转载自:http://www.net031.com/?p=50
-
标签:javascript、cookie 发布于:2009-01-09 12:48:00
当前共有0条评论,我也要发表: