在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自动消失

QUOTE:
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" : "");
}
//-->
2.获取cookie值:getCookie(name)
name:需要返回的cookie的name,如果不存在则返回空,其实现如下:

QUOTE:
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));
}
//-->
3.删除指定名称的cookie:deleteCookie(name, path, domain
删除指定名称的cookie

QUOTE:
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";
    }
}
//-->

4.完整代码演示

QUOTE:
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 1title>
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" οnclick='deleteCookie("name");return false;'>BR>
INPUT TYPE="submit" style="button" value="SetCookie" οnclick='setCookie("name", "Jason Davies");return false;'>BR>  
INPUT TYPE="submit" style="button" value="GetCookie" οnclick='alert(getCookie("name"));'>BR>
html>


QUOTE:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值