上一篇中我们学习了如何将大量的学习包含在一个cookie中.
另一种方法是使用多重cookies.
保存多重cookies的方法很直观.每一个cookie都有一个名称.
上一个例子中的cookie的名称是my_happy_cookie,我们可以
这样:
var the_cookie ="my_happy_cookie=happiness_and_joy";
document.cookie =the_cookie;
要保存多重cookie,只需给每个cookie一个不同的名字.如果
你要加入一个新的cookie,设置document.cookie 时并不会删
除前面已经设置了的cookies,所以:
var the_cookie ="my_happy_cookie=happiness_and_joy";
document.cookie = the_cookie;
var another_cookie= "my_other_cookie=more_joy_more_happiness";
document.cookie = another_cookie;
现在你需要访问这两个cookies,有些复杂,所以你需要明了整
个过程.假设你执行了上面的代码,现在想访问
my_happy_cookie.如果你查看document.cookie的内容,你会
看到:
my_happy_cookie=happiness_and_joy;
my_other_cookie=more_joy_more_happiness;
这样很直观,但是如果你想访问某个特定的cookie则有些困难. 数据挖掘研究院
下面的代码可以帮助你找出某个特定的cookie:
function WM_readCookie(name)
{
//如果没有cookie则返回false或者取得值并返回该值
if(document.cookie == ′′)
return false;
else
return
unescape(WM_getCookieValue(name));
}
function WM_getCookieValue(name)
{
// Declare variables.
var firstChar,lastChar;
// Get the entire cookie string.
// (This may have other
name=value pairs in it.)
var theBigCookie = document.cookie;
// Grab
just this cookie from theBigCookie string.
// Find the start of
′name′.
firstChar = theBigCookie.indexOf(name);
// If you found it,
if(firstChar != -1)
{
// skip ′name′ and ′=′.
firstChar +=
name.length + 1;
// Find the end of the value string (i.e. the next
′;′). 数据挖掘研究院
lastChar = theBigCookie.indexOf(′;′, firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
// Return the
value.
return theBigCookie.substring(firstChar, lastChar);
} else
{
// If there was no cookie, return false.
return false;
}
}
数据挖掘研究院
现在你已经学会了如何设置和读取基本的cookie.然而基本
的cookie常常在用户关闭他的浏览器时会被自动删除.有时候
这样最好因为通常的域只允许在用户的机器上保留20个cookie.
但是如果你希望将cookie保存在用户的机器上你需要设置一
个cookie失效的时间,它的格式是一种叫做GMT的特殊格式.
例如: 数据挖掘研究院
Mon, 27-Apr-1998 00:00:00 GMT
要正确设置GMT不是一件容易的事,你需要计算好某个日期是星
期几.好在Javascript有一个日期的方法叫做toGMTString可以
帮助你.下面是设定远期的某个时间的一个例子:
var the_date = new Date("December 31, 2023");
var the_cookie_date =the_date.toGMTString();
一旦你设置了你的cookie的失效期,你在必须在cookie设置的
前面加入这条信息.因此你的cookie应该如下:
cookie_name=blah_blah;expires=date
通常你只需在cookie字符串中加入expires=date,然后用分号
分割不同的cookie.
下面是一个如何建立有效期直至Mayan日历末尾的函数:
function
setCookie()
{
// get the information
//
var the_name = prompt("What′s your name?","");
var the_date = new Date("December 31, 2023");
var the_cookie_date =the_date.toGMTString();
// build and save the cookie
//
var the_cookie = "my_cookie=" + escape(the_name);
the_cookie = the_cookie +";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
最后cookie应该如下所示:
my_cookie=thau;expires=Fri,31-Dec-2023 00:00:00 GMT
设置好cookie之后,它将在用户的机器中国存在直到失效期.
如果你将某个cookie的失效期设置得比当前时间还早,该cookie
实际上不能在用户的机器上存活.
这是掌握cookie最后的一个障碍:缺省情况下cookie只能被在
同一个Web服务器上同一个路径下设置了该cookie的网页读取.
例如,如果在
"http://chimp.webmonkey.com/food/bananas/banana_puree.htm"
有一段Javascript询问了用户的姓名,你可能需要在你的另一
个网页例如主页中访问一个给定的名字.所以你必须设定该
cookie的路径.路径"path"用于设置可以读取一个cookie的最
顶层的目录.将cookie的路径设置为你的网页最顶层的目录可
以让该该目录下的所有网页都能访问该cookie. 数据挖掘实验室
方法:在你的cookie中加入path=/; 如果你只想让"food" 目录
中的网页可以使用该cookie,则你加入path=/food;.还有一点:
有些网站有许多小的域名,例如网猴可能还在
"chimp.webmonkey.com," "gorilla.webmonkey.com," 和
"ape.webmonkey.com." 域名下有网页.缺省情况下只有
"chimp.webmonkey.com" 域下的网页可以读取该cookie.如果
你向让"webmonkey.com"下的所有机器都可以读取该cookie,我
们必须在cookie中加入 "domain=webmonkey.com" .
要将一个cookie设置在
"http://chimp.webmonkey.com/food/bananas/banana_puree.htm"
并且让所有网猴的网页都可以利用它,我们可以这样:
function setCookie()
{
var
the_name = prompt("What′s your name?","");
var the_cookie =
"cookie_puss=" + escape(the_name) + ";" ;
var the_cookie = the_cookie
+ "path=/;";
var the_cookie = the_cookie + "domain=webmonkey.com;";
document.cookie = 数据挖掘研究院
the_cookie;
}

