9-1 讀寫?餅乾

@ӨAȤݪ JavaScript Ȥݪwаsʧ@AتOFnO@ȤݹqwиơAקKcN JavaScript {XӹȤݹqθƶi}aʧ@Cߤ@ҥ~ANO JavaScript iHbȤݪwЦsֶqơAoǸƺ٬p氮]Cookie^AjOMΤӤHơA`ΦpUG ϥ Cookie ӰOƪnBiHC|pUG
  1. ҦƧsbȤݹqA|ΦAwЪŶC
  2. P Cookie B⧡bȤݹqiA|W[ABtC
  3. ²ΡAiHϥΫȤݪ JavaScript ΦAݪ ASP Yi Cookie iŪgC
{]pvӻA Cookie ϥΤ]@ǤɲzQBG
  1. iaA]ȤݥiH Cookie \A JavaScript P Cookie {XNLkB@C]ɥ˴ Cookie \O_QAA줣P{qA]ӳy{XC^
  2. ȤᴫΤP~PsɡANLkѭsҼgJ Cookie TC
  3. Ȥ᭫qɡAN|y Cookie TyC
  4. ȤᴫxqɡACookie TNLkat@xqC
MWzIAO Cookie ϥ٬OܴMA]ثejs䴩 CookieAӥB@ϥΪ̤]Snh Cookie \C

ڭ̬ݬݦpˬdsO_} Cookie \AoiHg navigator.cookieEnabled ӧP_AШUCdҡG

Example]cookie01.htm^G

WzdҪlɦpUG

lɡ]cookie01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ˬds Cookie \O_Ұ</h2>
<hr>

<script>
document.write("navigator.cookieEnabled = " + navigator.cookieEnabled + "<br>");
if (navigator.cookieEnabled){
	document.write("Cookie \wgҰʡI");
	// b[Jϥ Cookie {X
} else {
	document.write("Cookie \|ҰʡI");
	alert("As]w䴩 CookieAХ}s Cookie \A~os̨ήĪGI");
	// b[Jϥ Cookie {X
}
</script>

<hr>
</body>
</html>

ѦiAڭ̥iHϥ navigator.cookieEnabled ӧP_ȤݪsO_} Cookie \AöiӿΤP{XӹFJw\C

Hint
Yn Cookie \Ab IE siHgѤUCUԦӭקGu/ںﶵ/pvC Cookie \AA˴ navigator.cookieEnabled ȡAO_pAQHH

㪺 Cookie OHUCrΦsbȤݪwСG

name=value;expires=expDate;
䤤 name N Cookie W١Avalue hO Cookie ȡAexpDate N Cookie ĴAYWLɶACookie N|QRCYSwĮɶAh Cookie u|QxsbO餤AbϥΪҦsAΦb session Oɡ]Session time-outAw]ȳq`O 20 ^ACookie N|Q۰ʧRFC

@ӺӨACookie O@ӦrAsb document.cookie rꤧAڭ̥iHϥΤUCdҨӦLX Cookie rꪺȡG

Example]cookie011.htm^G

bWzdҤAX{@ӦGѶüƲͪ name/value pairAoO ASP session ܼơAOѷLn IIS Web A۰ʳ]wTAΥHlܨCӨϥΪ̪ϥβߺDCWzdҪlɦpUG

lɡ]cookie011.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>CX Cookie r</h2>
<hr>

 Cookie rOG
<script>
document.write(document.cookie);
</script>

<hr>
</body>
</html>

Yn]w CookieAiUCdҡG

Example]cookie02.htm^G

WzdҪlɦpUG

lɡ]cookie02.htm^G]ǦϰUYi^
<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>
г]wp氮 name-value pairG<br>
Name: <input id=cookieName><br>
Value: <input id=cookieValue><br>
<input type=button value="gJ Cookie"
	onClick="document.cookie=escape(cookieName.value)+'='+escape(cookieValue.value); history.go(0)">
<p>Ъ`NGoǤp氮usbO餤A|H۩Ҧs Session OɦӮC
</form>

<hr>
<h3 align=center>p氮C</h3>
<script>listCookie();</script>

<hr>
</body>
</html>

bWzdҤAڭ̪N document.cookie ]w name+value ΦANiH[J@ Cookie]άO name/value pair^C

Hint
ڭ̨ϥ document.cookie="aaa=bbb" ɡAJavaScript ä|\N Cookie ơAӬONƥ[ document.cookie ݡAoOnSO`NBC

׶}ŮΨLiy~rAbs name value ɡA̦nϥ escape() unescape() ƨӶisXPѽXAHKױ㦳SNqrCHUO@Өϥ escape() dҡG

Example]escape01.htm^G

WzdҪlɦpUG

lɡ]escape01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ϥ escape() ӽsX</h2>
<hr>

<form>
sXerG<input id=source value="x y z"><br>
<input type=button value="isX" onClick="target.value=escape(source.value)"><br>
sXrG<input id=target>
</form>

<hr>
</body>
</html>

bWzdҤAŮQഫu%20vA@kONŮন ASCII XA]NO 32AMAN 32 ন16iAA[WʤANosX᪺u%20vC

Hint
Yϥ escape() 襤isXA䵲GOH unicode Ӫܪ 16 irCҦpusv|Qনu%u7DE8vA䤤 7DE8 NOusv unicode 16 iܪkC

~Ab cookie02.htm CXҦ Cookies ɡAWzdҩIsF@Ө listCookie()AƬOwqb cookieUtility.js CƹWAcookieUtility.js ]tFƺر`Ϊ򥻨ơAiH Cookie iUسBzAҦpG

`Bz Cookie {]pvӨAoǨ禡|``ΨAlXpUG

lɡ]cookieUtility.js^G]ǦϰUYi^
// 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 rG<p></b><center><font color=green>" + document.cookie + "</font></center>");
	document.writeln("<p><b>ѫ᪺ name/valueG</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 {]pPΡGΩΤ