--2007-6-18 13:00:36
在Javascript中管理你的Cookie
对于Cookie的操作大家肯定都很熟悉,asp,php,asp.net中都可以方便的处理,但其实并不是每次都要在后台进行处理的,我们熟悉的JavaScript也可以操作Cookie。cookie可以跨越一个域名下的多个网页,但不能跨越多个域名使用。注意:因为 Cookie 的值的要求是“只能用可以用在 URL 编码中的字符”。我们知道“escape()”方法是把字符串按 URL 编码方法来编码的,那我们只需要用一个“escape()”方法来处理输出到 Cookie 的值,用“unescape()”来处理从 Cookie 接收过来的值就万无一失了。而且这两个方法的最常用途就是处理 Cookies。其实设定一个 Cookie 只是“documents.cookie = 'cookieName=cookievalue'”这么简单,但是为了避免在 cookievalue 中出现 URL 里不准出现的字符,还是用一个 escape() 好。
设置Cookie时可以直接给document.cookie赋值:
document.cookie="url=http://www.linglihu.com";
document.cookie="name=linglihu";
1.添加一个cookie:addCookie(name,value,expires)
参数: name :cookie名称
value:cookie值
expires:过期时间,如果expire为0,当浏览器关闭时cookie自动消失
<script language="JavaScript" type="text/javascript">
<!--
function setCookie(name, value, expires, path, domain, secure) {
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
} //-->
</script>
2.获取cookie值:getCookie(name)
name:需要返回的cookie的name,如果不存在则返回空,其实现如下:
<script language="JavaScript" type="text/javascript">
<!--
function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//-->
</script>
3.删除指定名称的cookie:deleteCookie(name, path, domain
删除指定名称的cookie
<script language="JavaScript" type="text/javascript">
<!--
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";
}
} //-->
</script>
4.完整代码演示 <html><head><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>New Page 1</title></head><body></body><script type="Text/javascript">
/**
* Sets a Cookie with the given name and value.
* From http://www.linglihu.com/
* name Name of the cookie
* value Value of the cookie
* [expires] Expiration date of the cookie (default: end of current session)
* [path] Path where the cookie is valid (default: path of calling document)
* [domain] Domain where the cookie is valid
* (default: domain of calling document)
* [secure] Boolean value indicating if the cookie transmission requires a
* secure transmission
*/
function setCookie(name, value, expires, path, domain, secure) {
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
/**
* Gets the value of the specified cookie.
* From http://www.linglihu.com/
* name Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
* or null if cookie does not exist.
*/
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
/**
* Deletes the specified cookie.
* From http://www.linglihu.com/
* name name of the cookie
* [path] path of the cookie (must be same as path used to create cookie)
* [domain] domain of the cookie (must be same as domain used to create cookie)
*/
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";
}
}
</script>
<INPUT TYPE="submit" style="button" value="ClearCookie" onclick='deleteCookie("name");return false;'><BR>
<INPUT TYPE="submit" style="button" value="SetCookie" onclick='setCookie("name", "Jason Davies");return false;'><BR>
<INPUT TYPE="submit" style="button" value="GetCookie" onclick='alert(getCookie("name"));'><BR>
</html>
相关内容
下列内容与javascript有着内在的联系,建议您一并阅读。