📄 day2_10.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包含更多的信息,你可以将cookie的值<br> 设得很长.假设我们要保存某人的姓名,年龄和电话号码:</p> <p>var the_cookie = "username:thau/age:older than the hills/phone:411";<br> <br> document.cookie="my_happy_cookie=" + escape(the_cookie);<br> <br> 我用斜杠/来分割属性名称,用分号区别不同的属性名称及其<br> 属性值.斜杠/和分号是不是绝对的选择,你可以使用任何的<br> 字符做分割的标志:<br> <br> var the_cookie = "username=thau&age=older than the hills&phone=<br> 411";<br> <br> document.cookie="my_happy_cookie=" + escape(the_cookie);<br> <br> 你可以自行选择限位器.只要你注意在对cookie解码时也使用<br> 同样的限位器即可.<br> <br> 设置复杂的cookie时方法要复杂一些.我们建议你使用相关数<br> 组来保存所有的信息,假设我们将该cookie保存到某人的硬<br> 盘上:<br> <br> my_happy_cookie=username:thau/age:older than the hills/phone:411<br> <br> <br> 你可以将这些信息放到一个方便的相关数组中:<br> <br> function readTheCookie(the_info)<br> <br> {<br> <br> // load the cookie into a variable and unescape it<br> <br> var the_cookie = document.cookie;<br> <br> var the_cookie = unescape(the_cookie);<br> <br> // separate the values from the cookie name<br> <br> var broken_cookie = the_cookie.split("=");<br> <br> var the_values = broken_cookie[1];<br> <br> // break each name:value pair into an array<br> <br> var separated_values = the_values.split("/");<br> <br> // loop through the list of name:values and load<br> // up the associate array<br> <br> var property_value = "";<br> <br> for (loop = 0; loop <br> <br> <br> 如果在你的JavaScript中有上面这段代码,你可以这样调用它:<br> <br> var cookie_information = new Array();<br> <br> readTheCookie(cookie_information);<br> <br> 然后你就会正确设置了cookie_information["username"],<br> cookie_information["age"], 和cookie_information["phone"].<br> <br> 这些看起来可能有些难以理解,但实际上并不是很难.我们一<br> 步步分析:<br> var the_cookie = document.cookie; <br> 将cookie赋值给一个变量.<br> var the_cookie = unescape(the_cookie); <br> 取消escape()的编码<br> var broken_cookie = the_cookie.split("=");<br> var the_values = broken_cookie[1]; <br> 使the_values等同于username:thau/age:older than the hills/phone:411. <br> var separated_values = the_values.split("/"); <br> 生成一个包含3个元素名为separated_values的数组:<br> separated_values[0] = "username:thau"<br> separated_values[1] = "age:older than the hills"<br> separated_values[2] = "phone:411"<br> <br> for (loop = 0; loop <br> 循环调用separated_values的3个元素.<br> property_value = separated_values[loop]; <br> 提取当前的name:value配对,第1个配对是username:thau.<br> var broken_info = property_value.split(":"); <br> 将该配对分成名为broken_info的数组中的两个元素: <br> broken_info[0] = "username"<br> broken_info[1] = "thau"<br> <br> var the_property = broken_info[0]; <br> 第1次经过这个循环是,the_property是"username" <br> var the_value = broken_info[1]; <br> 其值是"thau" <br> the_info[the_property] = the_value; <br> 这里开始发回相关数组的便捷功能.它使得the_info<br> ["username"] = "thau",所以现在当你需要从cookie中查找<br> username时你只需:<br> var the_name = the_info["username"];<br> <br> 每次经过这个循环时,就在the_info中加入一个新元素.循<br> 环到最后时, the_info["username"] = "thau", the_info<br> ["age"] = "old as the hills" ,而 the_info["phone"] = 411.<br> <br> 有些烦琐,但是当你需要从cookie中输出入大量信息时这是一<br> 个很好的办法.当然还有别的办法.<a href="day2_11.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> 复杂的cookies读取<br> <font color="#FF0000">第十一页</font> <a href="day2_11.html">读取和编写多重cookies</a><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 + -