?? 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,設(shè)置document.cookie 時并不會刪<br>
除前面已經(jīng)設(shè)置了的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>
現(xiàn)在你需要訪問這兩個cookies,有些復雜,所以你需要明了整<br>
個過程.假設(shè)你執(zhí)行了上面的代碼,現(xiàn)在想訪問<br>
my_happy_cookie.如果你查看document.cookie的內(nèi)容,你會<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可以做的真正酷的內(nèi)容。<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">相關(guān)數(shù)組</a><br>
<font color="#FF0000">第六頁</font> <a href="day2_6.html">相關(guān)數(shù)組的一個例子</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">本文根據(jù)
網(wǎng)猴 相關(guān)文章改編,版權(quán)歸原作者所有。</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 + -