9-2 �]�w���Ĥ��

§Ú­Ì°£¤F¥i¥H³]©w Cookie ¤§¥~¡A·íµM¤]¥i¥H¹ï Cookie ¶i¦æ§R°£¡B­×§ï©Î¬d¸ßµ¥°Ê§@¡A¤U¦C½d¨Ò´N¬O¤@­Ó¡u¤p»æ°®¸ÕÅç³õ¡v¡A¥¦¤Þ¥Î¤F cookieUtility.js ¤ºªº¨ç¼Æ¨Ó¹ï Cookie ¶i¦æ·s¼W¡B­×§ï¡B§R°£ªº¦UºØ°Ê§@¡G

Example¡]cookie03.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]cookie03.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¤p»æ°®¸ÕÅç³õ</h2>
<hr>

<script src="cookieUtility.js"></script>
<input type=button value=³]©w¤p»æ°® onClick="setCookie(cookieName1.value, cookieValue1.value); history.go(0)">
Name: <input id=cookieName1> Value: <input id=cookieValue1><br>
<input type=button value=²¾°£¤p»æ°® onClick="delCookie(cookieName2.value); history.go(0)">
Name: <input id=cookieName2><br>
<input type=button value=¬d¸ß¤p»æ°® onClick="cookieValue3.value=getCookie(cookieName3.value)">
Name: <input id=cookieName3> Value: <input id=cookieValue3>
<h3>¥»ºô­¶ªº¤p»æ°®¦Cªí</h3>
<script>listCookie();</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì©I¥s¤F´X­Ó¨ç¼Æ¡]setCookie(), delCookie(), getCookie()µ¥¡^¨Ó¹ï Cookie ¶i¦æ³B²z¡A³o¨Ç¨ç¼Æ§¡©w¸q©ó cookieUtility.js ¤§¤¤¡A¥Ñ©ó½g´T¦³­­¡A½Ð¨£«e¤@¤p¸`ªº­ì©l½X¦Cªí¡A¦b¦¹¤£¦A­«½Æ¡C¯S§O­nª`·Nªº¬O¡A·í§Ú­Ì­n§R°£¤@­Ó Cookie¡A³q±`ªº§@ªk¬O³]©w¨ä¥¢®Ä¤é´Á¡A¥u­n¥¢®Ä¤é´Á¬O¦­©ó²{¦bªº®É¶¡¡ACookie ´N·|³Q§R°£¤F¡A¦b cookieUtility.js ¤¤ªº delCookie() ¨ç¼Æ¡A´N¬O¥H¦¹¤èªk¶i¦æ¹ê§@¡A¦¹¨ç¼Æªºµ{¦¡½X¦p¤U¡G

// Delete cookie entry function delCookie(name) { var expDate = new Date(); expDate.setTime(expDate.getTime()-1); // ³]©w Cookie ªº¥¢®Ä®É¶¡¤ñ¥Ø«e®É¶¡ÁÙ¦­ document.cookie = escape(name) + "=; expires=" + expDate.toGMTString(); // ­«·s³]©w Cookie }

¦b«e¤@­Ó½d¨Ò¤¤¡A¥Ñ©ó¦b³]©w Cookie ®É¡A¨S¦³«ü©w¨äªº´Á­­¡A¦]¦¹ Cookie ¥u³Q¦s©ñ¦b°O¾ÐÅ餤¡A­Y¨Ï¥ÎªÌÃö³¬©Ò¦³ÂsÄý¾¹µøµ¡¡ACookie ªº¸ê®Æ±NÀH¤§®ø¥¢¡C¡]½Ð¸Õ¸Õ¬Ý¡I¡^­Y¨Ï¥ÎªÌ¤£¦A¦s¨ú¸Óºô­¶¡A«h Cookie ¤]·|¦b Session Time-out ¡]¥Ñ¦øªA¾¹³]©w¡A³q±`¬O 20 ¤ÀÄÁ¡^«á´N®ø¥¢¡C

­Y­n¨Ï Cookie ¯àºû«ùªø¤[ªº®Ä¤O¡A´N¥²¶·³]©w Cookie ªº¥¢®Ä¤é´Á¡]Expiration date¡^¡C´«¥y¸Ü»¡¡A¦b¦¹¥¢®Ä¤é´Á«e¡ACookie ³£¬O¦³®Äªº¡A¨Ã·|¦bÂsÄý¾¹¸ü¤Jºô­¶®É¡A¦s©ñ©ó document.cookie¡A­YÂsÄý¾¹Ãö³¬¡A«h·s¼Wªº Cookie ¡]±a¦³¯S©wªº¥¢®Ä¤é´Á¡^·|³Q¦s©ñ¨ì¯S©wªºÀɮפ¤¡CCookie ¦s©ñªºÀɮסA¦b Netscape ¬O cookies.txt¡A¦b IE «h¬O¦b Cookies ¥Ø¿ý¤§¤U¡C±ý±oª¾³o¨ÇÀɮשΥؿýªº¹êÅé¸ô®|¡A½Ðª½±µ¶i¦æ¡u´M§äÀɮסv§Y¥i¡C

¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î Cookie ¨Ó°O¿ý¨Ï¥ÎªÌ³y³Xºô­¶ªº¦¸¼Æ¡G

Example¡]cookie04.htm¡^¡G

¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

­ì©lÀÉ¡]cookie04.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>§Q¥Î Cookie °O¿ý«È¤á«ô³Xºô­¶¦¸¼Æ</h2>
<hr>

<script src="cookieUtility.js"></script>
<script>
duration = 365;			// ¸ê®Æ±N³Q«O¯d¤@¦~
today = new Date();
expireDate = new Date();
expireDate.setTime(today.getTime()+1000*60*60*24*duration);	// Set up expire date
cookieName = 'VisitCount';
count = getCookie(cookieName);
if (count==null)
	count=0;
count++;
if (count==1)
	message="ÁÂÁ±zªì¦¸¥úÁ{´HªÙ¡I";
else if (count==2)
	message="ÁÂÁ±z¦A«×¥úÁ{¡I";
else
	message="±z¤w¸g¥úÁ{¥»¯¸ "+count+" ¦¸¤F¡I"
setCookie(cookieName, count, expireDate);
document.write(message);
document.write("¡]±z¦b¥»­¶©Ò¯d¤Uªº¸ê®Æ°O¿ý¦b¦W¬°¡u"+cookieName+"¡vªº¤p»æ°®¡A±N·|³Q«O¯d"+duration+"¤Ñ¡C¡^");
</script>
<p>ÂI¿ï <input type="button" value="Âk¹s" onClick="delCookie('VisitCount'); history.go(0)"> ¨Ó¨Ï­p¼Æ¸ê®ÆÂk¹s¡C<br>
ÂI¿ï <input type="button" value="­«¸ü" onClick="history.go(0)"> ¨Ó¼W¥[°O¼Æ¸ê®Æ¡C

<hr>
<h3 align=center>¥»­¶¤p»æ°®¦Cªí</h3>
<script src="listCookie.js"></script>
<script>listCookie();</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A³y³Xºô­¶ªº­p¼Æ¸ê®Æ¬O¦s©ñ¦b¦W¬° VisitCount ªº Cookie ¤§¤¤¡A¦¹ Cookie ªº¦³®Ä´Á¶¡¬O¤@¦~¡A¦]¦¹¨Ã¤£·|ÀHµÛÂsÄý¾¹ªºÃö³¬¦Ó®ø¥¢¡C¡]½Ð¸Õ¸Õ¬Ý¡I¡^

¥t¥~¡A¦b¦P¤@­Ó¥Ø¿ý¤Uªººô­¶¡A°ò¥»¤W¥i¥H¦@¥Î Cookie ªº¸ê°T¡C´«¥y¸Ü»¡¡A¥u­n cookie02.htm ©M cookie03.htm ¬O¦ì©ó¦P¤@­Ó¥Ø¿ý¡A§A¥i¥H¦b cookie02.htm ³]©w¤@­Ó Cookie¡AµM«á¦b cookie03.htm ªº Cookie ¦Cªí¬Ý¨ì¤§«e³]©wªº¸ê®Æ¡C


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ