9-1 Ū�g�p�氮

¤@¯ë¦Ó¨¥¡A«È¤áºÝªº JavaScript ¤£¯à¹ï«È¤áºÝªºµwºÐ°µ¥ô¦ó¦s¨úªº°Ê§@¡A¦¹­­¨îªº¥Øªº¬O¬°¤F­n«OÅ@«È¤áºÝ¹q¸£ªºµwºÐ¸ê®Æ¡AÁקK¦³´c·Nªº JavaScript µ{¦¡½X¨Ó¹ï«È¤áºÝ¹q¸£©Î¸ê®Æ¶i¦æ¯}Ãaªº°Ê§@¡C°ß¤@ªº¨Ò¥~¡A´N¬O JavaScript ¥i¥H¦b«È¤áºÝªºµwºÐ¦s¨ú·¥¤Ö¶qªº¦³­­¸ê®Æ¡A³o¨Ç¸ê®ÆºÙ¬°¤p»æ°®¡]Cookie¡^¡A¤j³¡¤À³£¬O©M¥Î¤á¬ÛÃöªº­Ó¤H¸ê®Æ¡A±`¨£ªº¬ÛÃöÀ³¥Î¦p¤U¡G ¨Ï¥Î Cookie ¨Ó°O¿ý¸ê®Æªº¦n³B¥i¥H¦CÁ|¦p¤U¡G
  1. ©Ò¦³¸ê®Æ§¡¦s©ñ¦b«È¤áºÝ¹q¸£¡A¤£·|¦û¥Î¦øªA¾¹µwºÐªÅ¶¡¡C
  2. »P Cookie ¬ÛÃöªº¹Bºâ§¡¦b«È¤áºÝ¹q¸£¶i¦æ¡A¤£·|¼W¥[¦øªA¾¹¹Bºâ­t¸ü¡C
  3. ²³æ©ö¥Î¡A¥i¥H¨Ï¥Î«È¤áºÝªº JavaScript ©Î¦øªA¾¹ºÝªº ASP §Y¥i¹ï Cookie ¶i¦æŪ¼g¡C
¦ý¹ï©óºô­¶µ{¦¡³]­p®v¨Ó»¡¡A Cookie ªº¨Ï¥Î¤]¦³¤@¨Ç¤£ºÉ²z·Q¤§³B¡G
  1. ¤£¥i¾a¡A¦]«È¤áºÝ¥i¥H§¹¥þÃö³¬ Cookie ªº¥\¯à¡A¦¹®É JavaScript »P Cookie ¬ÛÃöªºµ{¦¡½X´NµLªk¹B§@¡C¡]¦¹®É¥²¶·¥ýÀË´ú Cookie ¥\¯à¬O§_³QÃö³¬¡A¦A¸õ¨ì¤£¦Pªºµ{¦¡¤ù¬q¡A¦]¦Ó³y¦¨µ{¦¡½Xªº½ÆÂø¡C¡^
  2. «È¤á´«¥Î¤£¦P«~µPªºÂsÄý¾¹®É¡A´NµLªk§ì¨ì¥Ñ­ì¨ÓÂsÄý¾¹©Ò¼g¤Jªº Cookie ¸ê°T¡C
  3. «È¤á­«Äé¹q¸£®É¡A´N·|³y¦¨ Cookie ¸ê°Tªº¬y¥¢¡C
  4. «È¤á´«¥x¹q¸£®É¡ACookie ªº¸ê°T´NµLªk±a¨ì¥t¤@¥x¹q¸£¡C
ÁöµM¦³¤W­zªº¯ÊÂI¡A¦ý¬O Cookie ªº¨Ï¥ÎÁÙ¬O«Ü´¶¹M¡A¦]¬°¥Ø«e¤j³¡¤ÀªºÂsÄý¾¹³£¤ä´© Cookie¡A¦Ó¥B¤@¯ë¨Ï¥ÎªÌ¤]¨S¦³¥²­n¥hÃö³¬ Cookie ªº¥\¯à¡C

­º¥ý§Ú­Ì¬Ý¬Ý¦p¦óÀˬdÂsÄý¾¹¬O§_¶}±Ò Cookie ¥\¯à¡A³o¥i¥H¸g¥Ñ navigator.cookieEnabled ¨Ó§PÂ_¡A½Ð¨£¤U¦C½d¨Ò¡G

Example¡]cookie01.htm¡^¡G

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

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

<body>
<h2 align=center>ÀˬdÂsÄý¾¹ªº Cookie ¥\¯à¬O§_±Ò°Ê</h2>
<hr>

<script>
document.write("navigator.cookieEnabled = " + navigator.cookieEnabled + "<br>");
if (navigator.cookieEnabled){
	document.write("Cookie ¥\¯à¤w¸g±Ò°Ê¡I");
	// ¦b¦¹¥[¤J¨Ï¥Î Cookie ªºµ{¦¡½X
} else {
	document.write("Cookie ¥\¯à©|¥¼±Ò°Ê¡I");
	alert("§AªºÂsÄý¾¹³]©w¤£¤ä´© Cookie¡A½Ð¥ý¶}±ÒÂsÄý¾¹ªº Cookie ¥\¯à«á¡A¤~¯à±o¨ìÂsÄý¥»ºô­¶ªº³Ì¨Î®ÄªG¡I");
	// ¦b¦¹¥[¤J¤£¨Ï¥Î Cookie ªºµ{¦¡½X
}
</script>

<hr>
</body>
</html>

¥Ñ¦¹¥iª¾¡A§Ú­Ì¥i¥H¨Ï¥Î navigator.cookieEnabled ¨Ó§PÂ_«È¤áºÝªºÂsÄý¾¹¬O§_¶}±Ò Cookie ªº¥\¯à¡A¨Ã¶i¦Ó¿ï¥Î¤£¦Pªºµ{¦¡½X¨Ó¹F¨ìºô­¶ªº¬J©w¥\¯à¡C

Hint
­Y­nÃö³¬ Cookie ªº¥\¯à¡A¦b IE ÂsÄý¾¹¥i¥H¸g¥Ñ¤U¦C¤U©Ô¦¡¿ï³æ¨Ó­×§ï¡G¤u¨ã/ºô»Úºô¸ô¿ï¶µ/Áô¨pÅv¡C½ÐÃö³¬ Cookie ¥\¯à«á¡A¦AÀË´ú navigator.cookieEnabled ªº­È¡A¬O§_¦p§A·Q¹³¡H¬°¤°»ò¡H

§¹¾ãªº Cookie ¬O¥H¤U¦C¦r¦ê§Î¦¡¦s©ñ¦b«È¤áºÝªºµwºÐ¡G

name=value;expires=expDate;
¨ä¤¤ name ¥Nªí Cookie ªº¦WºÙ¡Avalue «h¬O¹ïÀ³ªº Cookie ­È¡AexpDate ¥Nªí Cookie ªº¦³®Ä´Á¶¡¡A­Y¶W¹L¦¹®É¶¡¡ACookie ´N·|³Q§R°£¡C­Y¨S¦³«ü©w¦³®Ä®É¶¡¡A«h Cookie ¥u·|³QÀx¦s¦b°O¾ÐÅ餤¡A¦b¨Ï¥ÎªÌÃö±¼©Ò¦³ªºÂsÄý¾¹«á¡A©Î¦b session ¹O®É¡]Session time-out¡A¹w³]­È³q±`¬O 20 ¤ÀÄÁ¡^«á¡ACookie ´N·|³Q¦Û°Ê§R°£¤F¡C

¹ï©ó¥ô¤@­Óºô­¶¦Ó¨¥¡ACookie ¬O¤@­Ó¦r¦ê¡A¦s©ñ¦b document.cookie ¦r¦ê¤§¤¤¡A§Ú­Ì¥i¥H¨Ï¥Î¤U¦C½d¨Ò¨Ó¦L¥X Cookie ¦r¦êªº­È¡G

Example¡]cookie011.htm¡^¡G

¦b¤W­z½d¨Ò¤¤¡A¥X²{¤@­Ó¦ü¥G¥Ñ¶Ã¼Æ²£¥Íªº name/value pair¡A³o¬O ASP ªº session ÅܼơA¬O¥Ñ·L³n IIS Web ¦øªA¾¹¦Û°Ê³]©wªº¸ê°T¡A¥Î¥H°lÂܨC­Ó¨Ï¥ÎªÌªº¨Ï¥Î²ßºD¡C¤W­z½d¨Òªº­ì©lÀɦp¤U¡G

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

<body>
<h2 align=center>¦C¥X Cookie ¦r¦ê</h2>
<hr>

¥»­¶ªº Cookie ¦r¦ê¬O¡G
<script>
document.write(document.cookie);
</script>

<hr>
</body>
</html>

­Y­n³]©w Cookie¡A¥i¨£¤U¦C½d¨Ò¡G

Example¡]cookie02.htm¡^¡G

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

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

<body>
<h2 align=center>³]©w¨ÃÅã¥Ü Cookie ªº½d¨Ò</h2>
<hr>

<script src="cookieUtility.js"></script>
<form>
½Ð³]©w¤p»æ°®ªº name-value pair¡G<br>
Name: <input id=cookieName><br>
Value: <input id=cookieValue><br>
<input type=button value="¼g¤J Cookie"
	onClick="document.cookie=escape(cookieName.value)+'='+escape(cookieValue.value); history.go(0)">
<p>½Ðª`·N¡G³o¨Ç¤p»æ°®¥u¦s©ñ¦b°O¾ÐÅ餤¡A·|ÀHµÛ©Ò¦³ÂsÄý¾¹ªºÃö³¬©Î Session ¹O®É¦Ó®ø¥¢¡C
</form>

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

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ìª½±µ±N document.cookie ³]©w¬° name+value ªº§Î¦¡¡A´N¥i¥H¥[¤J¤@­Ó Cookie¡]©Î¬O name/value pair¡^¡C

Hint
·í§Ú­Ì¨Ï¥Î document.cookie="aaa=bbb" ®É¡AJavaScript ¨Ã¤£·|»\±¼­ì¥ý´N¦³ªº Cookie ¸ê®Æ¡A¦Ó¬O±N¬ÛÃö¸ê®Æ¥[¨ì document.cookie ªº§ÀºÝ¡A³o¬O­n¯S§Oª`·N¤§³B¡C

¬°Á׶}ªÅ®æ©Î¨ä¥L¥i¯à³y¦¨¿ù»~¤§¦r¤¸¡A¦b¦s¨ú name ©Î value ®É¡A³Ì¦n¨Ï¥Î escape() ¤Î unescape() ¨ç¼Æ¨Ó¶i¦æ½s½X»P¸Ñ½X¡A¥H«KÁ×±¼¨ã¦³¯S®í·N¸qªº¦r¤¸¡C¥H¤U¬O¤@­Ó¨Ï¥Î escape() ªº½d¨Ò¡G

Example¡]escape01.htm¡^¡G

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

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

<body>
<h2 align=center>¨Ï¥Î escape() ¨Ó½s½X</h2>
<hr>

<form>
½s½X«e¦r¦ê¡G<input id=source value="x y z"><br>
<input type=button value="¶i¦æ½s½X" onClick="target.value=escape(source.value)"><br>
½s½X«á¦r¦ê¡G<input id=target>
</form>

<hr>
</body>
</html>

¦b¤W­z½d¨Ò¤¤¡AªÅ®æ³QÂà´«¦¨¡u%20¡v¡A¨ä§@ªk¬O¥ý±NªÅ®æÂন ASCII ½X¡A¤]´N¬O 32¡AµM«á¦A±N 32 Âন16¶i¦ì¡A¦A¥[¤W¦Ê¤À¤ñ¡A´N±o¨ì½s½X«áªº¡u%20¡v¡C

Hint
­Y¨Ï¥Î escape() ¹ï¤¤¤å¶i¦æ½s½X¡A¨äµ²ªG¬O¥H unicode ¨Óªí¥Üªº 16 ¶i¦ì¦r¦ê¡C¨Ò¦p¡u½s¡v·|³QÂন¡u%u7DE8¡v¡A¨ä¤¤ 7DE8 ´N¬O¡u½s¡vªº unicode ªº 16 ¶i¦ìªí¥Üªk¡C

¦¹¥~¡A¦b cookie02.htm ¦C¥X©Ò¦³ªº Cookies ®É¡A¤W­z½d¨Ò©I¥s¤F¤@­Ó¨ç¼Æ listCookie()¡A¦¹¨ç¼Æ¬O©w¸q¦b cookieUtility.js ¤§¤¤¡C¨Æ¹ê¤W¡AcookieUtility.js ¥]§t¤F¼ÆºØ±`¥Îªº°ò¥»¨ç¼Æ¡A¥i¥H¹ï Cookie ¶i¦æ¦UºØ³B²z¡A¨Ò¦p¡G

¹ï©ó±`³B²z Cookie ªºµ{¦¡³]­p®v¦Ó¨¥¡A³o¨Ç¨ç¦¡³£·|±`±`¥Î¨ì¡A­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]cookieUtility.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// »P cookie ¬ÛÃöªº¦UºØ¨ç¼Æ
// Set cookie
function setCookie(name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : null;

	document.cookie = escape(name) + "=" + escape(value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == null) ? "" : ("; secure=" + secure));
}

// 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
//	alert(document.cookie);
}

// Get cookie by name
function getCookie(name) {
	var arg = escape(name) + "=";
	var nameLen = arg.length;
	var cookieLen = document.cookie.length;
	var i = 0;
   
	while (i < cookieLen) {
		var j = i + nameLen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieValueByIndex(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

// Show the cookie string
function showAllCookie() {
	alert(document.cookie);
}

function getCookieValueByIndex(startIndex) {
	var endIndex = document.cookie.indexOf(";", startIndex);
	if (endIndex == -1) 
		endIndex = document.cookie.length;
	return unescape(document.cookie.substring(startIndex, endIndex));
}

// List all name/value pairs in a table
function listCookie() {
	document.writeln("<p><b>­ì©l Cookie ¦r¦ê¡G<p></b><center><font color=green>" + document.cookie + "</font></center>");
	document.writeln("<p><b>©î¸Ñ«áªº name/value¡G</b>");
	document.writeln("<table border=1 align=center>");
	document.writeln("<tr><th>Name<th>Value");
	cookieArray = document.cookie.split(";");
	for (var i=0; i<cookieArray.length; i++) {
		thisCookie = cookieArray[i].split("=");
		cookieName = unescape(thisCookie[0]);
		cookieValue = unescape(thisCookie[1]);
		document.writeln("<tr><td><font color=red>"+cookieName+"</font><td><font color=green>"+cookieValue+"</font>");
	}
	document.writeln("</table>");
}

// Delete all cookies
function deleteAllCookie() {
	var cookieArray = document.cookie.split(";");
	for (var i=0; i<cookieArray.length; i++) {
		thisCookie = cookieArray[i].split("=");
		cookieName = unescape(thisCookie[0]);
		delCookie(cookieName); 
	}
}


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