📄 day2_11.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312-80">
<style type="text/css">
<!--
a:link { color: blue; text-decoration: none}
a:visited { color: purple; text-decoration: none}
a:hover { color: #CC0033; text-decoration: underline}
-->
</style>
<title>JavaScript高级教程</title>
</head>
<body topmargin="1" leftmargin="2">
<table border="0" width="591" cellspacing="0">
<tr>
<td bgcolor="#ffff99" width="451">JavaScript高级教程 - 第二课</td>
</tr>
<tr>
<td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td>
</tr>
</table>
<div align="left">
<table border="0" width="630" cellspacing="0">
<tr>
<td width="458" valign="top" align="left" rowspan="2"><small><small><br>
</small></small><strong>第十一页:读取和编写多重cookies</strong>
<p>上一篇中我们学习了如何将大量的学习包含在一个cookie中.<br>
另一种方法是使用多重cookies.<br>
<br>
保存多重cookies的方法很直观.每一个cookie都有一个名称.<br>
上一个例子中的cookie的名称是my_happy_cookie,我们可以<br>
这样:<br>
var the_cookie ="my_happy_cookie=happiness_and_joy";<br>
<br>
document.cookie =the_cookie;<br>
<br>
要保存多重cookie,只需给每个cookie一个不同的名字.如果<br>
你要加入一个新的cookie,设置document.cookie 时并不会删<br>
除前面已经设置了的cookies,所以:<br>
<br>
var the_cookie ="my_happy_cookie=happiness_and_joy";<br>
<br>
document.cookie = the_cookie;<br>
<br>
var another_cookie= "my_other_cookie=more_joy_more_happiness";<br>
<br>
document.cookie = another_cookie;<br>
<br>
现在你需要访问这两个cookies,有些复杂,所以你需要明了整<br>
个过程.假设你执行了上面的代码,现在想访问<br>
my_happy_cookie.如果你查看document.cookie的内容,你会<br>
看到:<br>
<br>
my_happy_cookie=happiness_and_joy;<br>
my_other_cookie=more_joy_more_happiness;<br>
<br>
这样很直观,但是如果你想访问某个特定的cookie则有些困难.<br>
下面的代码可以帮助你找出某个特定的cookie:<br>
function WM_readCookie(name)<br>
{<br>
<br>
//如果没有cookie则返回false或者取得值并返回该值<br>
if(document.cookie == '')<br>
return false;<br>
else<br>
return<br>
unescape(WM_getCookieValue(name));<br>
}<br>
<br>
<br>
<br>
function WM_getCookieValue(name)<br>
{<br>
<br>
// Declare variables.<br>
<br>
var firstChar,lastChar;<br>
<br>
// Get the entire cookie string.<br>
// (This may have other<br>
name=value pairs in it.)<br>
<br>
var theBigCookie = document.cookie;<br>
<br>
// Grab<br>
just this cookie from theBigCookie string.<br>
<br>
// Find the start of<br>
'name'.<br>
<br>
firstChar = theBigCookie.indexOf(name);<br>
<br>
// If you found it,<br>
<br>
<br>
if(firstChar != -1)<br>
{<br>
<br>
// skip 'name' and '='.<br>
<br>
firstChar +=<br>
name.length + 1;<br>
<br>
// Find the end of the value string (i.e. the next<br>
';').<br>
<br>
lastChar = theBigCookie.indexOf(';', firstChar);<br>
<br>
<br>
if(lastChar == -1) lastChar = theBigCookie.length;<br>
<br>
// Return the<br>
value.<br>
<br>
return theBigCookie.substring(firstChar, lastChar);<br>
<br>
} else<br>
{<br>
<br>
// If there was no cookie, return false.<br>
<br>
return false;<br>
<br>
<br>
<br>
}<br>
<br>
}<br>
<br>
下面我们将学习一下cookies可以做的真正酷的内容。<a href="day2_12.html">>></a></p>
<p><font face="宋体" size="3" color="#000000"><strong>JavaScript高级教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
</font><font color="#FF0000">第一页</font> <a href="day2_1.html">Javascript高级教程-第2日</a><br>
<font color="#FF0000">第二页</font> <a href="day2_2.html">神奇的字符串处理</a><br>
<font color="#FF0000">第三页</font> <a href="day2_3.html">子字符串</a><br>
<font color="#FF0000">第四页</font> <a href="day2_4.html">分割方法(splitting
method)</a><br>
<font color="#FF0000">第五页</font> <a href="day2_5.html">相关数组</a><br>
<font color="#FF0000">第六页</font> <a href="day2_6.html">相关数组的一个例子</a><br>
<font color="#FF0000">第七页</font> <a href="day2_7.html">介绍cookie</a><br>
<font color="#FF0000">第八页</font> <a href="day2_8.html">深入了解cookies</a><br>
<font color="#FF0000">第九页</font> <a href="day2_9.html">读取cookies</a><br>
<font color="#FF0000">第十页</font> <a href="day2_10.html">复杂的cookies读取</a><br>
<font color="#FF0000">第十一页</font> 读取和编写多重cookies<br>
<font color="#FF0000">第十二页</font> <a href="day2_12.html">再次深入了解cookies</a><br>
<font color="#FF0000">第十三页</font> <a href="day2_13.html">cookie路径和域</a></p>
<p><font size="3">[<a href="day1_1.html">第1课</a>][第2课][<a href="day3_1.html">第3课</a>][<a href="day4_1.html">第4课</a>][<a href="day5_1.html">第5课</a>]</font></p>
<hr align="left">
<!--webbot bot="Include" U-Include="../../copyright.html" TAG="BODY" startspan -->
<p><font face="verdana, arial, geneva, sans-serif" size="2"><a href="http://phtshop.yeah.net" target="_top">本文根据
网猴 相关文章改编,版权归原作者所有。</a> </font><font color="#000000"><span class="smallfont"></span></font></p>
<!--webbot bot="Include" endspan i-checksum="15926" --> </td>
</tr>
<tr> </tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -