分享

使用javascript操作cookie

 I_T_馆 2014-03-24
效果图:
----------------------------------------------------------------

 
 

cookie.js
--------------------------------------------------------------
/**
 * Read the JavaScript cookies tutorial at:
 *   http://www./articles/javascript/cookies.xml
 */

/**
 * Sets a Cookie with the given name and value.
 * 创建cookie
 * name       Name of the cookie cookie的name
 * value      Value of the cookiecookie的值
 * [expires]  过期时间,没有则关闭浏览器过期,如果想让一个cookie删除,则设置过期时间为过去的某一个时间即可
 * [path]    path="/www",设置了cookie能够被服务器里的www目录及子目录访问,这样通过path设置cookie共享的范围.如果设置cookie能够被服务器上所有的网页访问到,达到全局共享的目的,则可以改变path为path="/"
 * [domain]   跨服务器共享.有多个网站分布在不同的服务器上,但他们属于同一个用户.例如,www.baihui.com.其办公网站office.baihui.com,其登陆网站ipassport.baihui.com.他们是统一用户,因此需要实现cookie共享.可以设置domain=".baihui.com"
 * [secure]   是信息传输更加安全.secure=true,设置cookie只能在安全的Internet上连接.
 */
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.
 * 读取cookie
 * 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.
 * 删除cookie
 * 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";
    }
}
------------------------------------------------------------------------------------

调用cookie.js的html文件:cookies.html
-------------------------------------------------------

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>cookies</title>
<script src="cookie.js"></script>
<script language="JavaScript">
<!--
var exp  = new Date("December 31, 9998"); 
setCookie("COOKIE_MYNAME","Tom",exp,"/");

var myname = getCookie("COOKIE_MYNAME");
document.write("设置的名为COOKIE_MYNAME的cookies值为:"+myname + "<br>");

deleteCookie("COOKIE_MYNAME","/");
var newname = getCookie("COOKIE_MYNAME");
document.write("设置的名为COOKIE_MYNAME的cookies值为:"+newname);
//-->
</script>
</head>
<body>
</body>
</html>

--------------------------------------------------------

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多