5-2 ��Ʃw�q�ɮת��ϥ�

­Y­n­«½Æ¨Ï¥Î JavaScript ªº¨ç¼Æ¡A¥i±N³o¨Ç±`¥Îªº¨ç¼Æ¶×¾ã¨ì¤@­Ó¨ç¼Æ©w¸qÀɮסA¦A¥H¤U¦C¤è¦¡¨Ó±N¨ç¼Æµ{¦¡½X¥[¤Jºô­¶¤§¤¤¡G
<script src="¨ç¼Æ©w¸qÀÉ®×.js"></script>
¤W­z½d¨Ò¤¤ªº¨ç¼Æ©w¸qÀɪº°ÆÀɦW¬O "js"¡A³o¥u¬O¤@¯ë²ßºD¡A¨Ï¥ÎªÌ¤]¥i¥H¿ï¥Î¨ä¥¦¥ô·N°ÆÀɦW¡C¦ý¬O­Y§ä¤£¨ìÀɮסAºô­¶¨Ã¤£·|¦L¥X¿ù»~°T®§¡A³o¬O­n¯S§Oª`·Nªº¦a¤è¡C

¨Ï¥Î¨ç¼Æ©w¸qÀɪº¦n³B¬O¡G

¥H¤U³o­Ó½d¨Ò¡A´N¬O¨Ï¥Î³oºØ¤èªk¨Ó¥]§t¤@­Ó JavaScript ªº¨ç¼Æ©w¸qÀɮסA¦¹Àɮשw¸q¤F¨â­Ó¨ç¼Æ¡A¥i¤À§O¶Ç¦^²{¦b®É¶¡»P¬P´Á´X¡Aºô­¶§e²{®ÄªG¦p¤U¡G

Example¡]timeDisplay02.htm¡^¡G

·í¨Ï¥ÎªÌ«ö¤U¡uÅã¥Ü²{¦b®É¶¡¡v®É¡AÂsÄý¾¹´N·|¥h©I¥s©w¸q©ó time.js ªº¨ç¼Æ currentTime() ¥H¨ú±o²{¦b®É¶¡¡A¨Ã±Nµ²ªGÅã¥Ü©óĵ§iµøµ¡¡C¦¹½d¨Ò­ì©l½X¦p¤U¡G

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

<body>
<h2 align=center>¨Ï¥Î¨ç¼Æ©w¸qÀɮרÓÅã¥Ü²{¦b®É¶¡</h2>
<hr>

<script src="time.js"></script>
<a href="javascript:alert('²{¦b¬O¡u' + currentTime()+ '¡v¡I')">Åã¥Ü²{¦b®É¶¡</a><br>
<a href="javascript:alert('¤µ¤Ñ¬O¡u' + currentDay() + '¡v¡I')">Åã¥Ü¬P´Á´X</a><br>

<hr>
</body>
</html>

¦b¤W¨Ò¤¤¡A§Ú­Ì¥æ¤¬¹B¥ÎÂù¤Þ¸¹ (") ¤Î³æ¤Þ¸¹ (')¡A¥H©ú¥Õ°Ï¤À href ©Ò¥Îªº¦r¦ê¤Î JavaScript ©Ò¥Îªº¦r¦ê¡C¤W¨Ò¥]§t¤F¤@­Ó JavaScript ªº¨ç¼Æ©w¸qÀÉ time.js¡A¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]time.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// »P¤é´Á¬ÛÃöªº¨ç¼Æ
function currentTime(){		// ¦^¶Ç²{¦bªº®É¶¡
	var today = new Date();
	var hour = today.getHours();
	var minute = today.getMinutes();
	var second = today.getSeconds();
	var prepand = (hour>12)? "¤U¤È":"¤W¤È";
	hour = (hour>12)? hour-12:hour;
	return(prepand + hour + " ÂI " + minute + " ¤À " + second + " ¬í");
}

function currentDay(){		// ¦^¶Ç¤µ¤Ñ¬P´Á´X
	var today = new Date();
	var day = today.getDay();	// ¨ú±o¤µ¤Ñ¬O¬P´Á´X
	var conversion=["¤Ñ", "¤@", "¤G", "¤T", "¥|", "¤­", "¤»"];
	return("¬P´Á"+conversion[day]);
}

¡]¦³Ãö©ó®É¶¡©M¤é´Áªº¥Îªk¡A½Ð¨£¥»®Ñ«e´X³¹ªº»¡©ú¡C¡^

§Ú­Ì¤]¥i¥H§ó½ÆÂøªº¨Ò¤l¡A¥i¨£¤U­±ªº¤å¥ó©Ê½è¦Cªí¡G

Example¡]docProp01.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

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

<body>
<h2 align=center>"document" ªº©Ê½è¦Cªí</h2>
<hr>
<a href="http://www.nthu.edu.tw">²M¤j­º­¶</a><br>
<a href="http://www.nctu.edu.tw">¥æ¤j­º­¶</a><br>
<a href="http://www.ntu.edu.tw">¥x¤j­º­¶</a>
<hr>

<script src="listProp.js"></script>
<p><h3>"document" ªº©Ê½è¦Cªí¡G</h3>
<p><script>listProp(document, "document")</script>

<hr>
</body>
</html>

¤W­z½d¨Ò¥]§t¤F¤@­Ó listProp.js ÀɮסA¨ä¤º®e¦p¤U¡G

­ì©lÀÉ¡]listProp.js¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
// ¦C¦Lª«¥ó©Ê½è
function listProp(obj, objName) {
	for (var i in obj)
		document.writeln(objName+".<font color=red>"+i+"</font> = <font color=green>"+obj[i]+"</font><br>");
}

¦b¤W­zªºµ{¦¡½X¤¤¡A§Ú­Ì¥Î¨ì¤F for-in °j°é¡A¦¹ºØ°j°é¯S§O¾A¥Î©ó¦C¥X¤@­Óª«¥óªº©Ò¦³©Ê½è¡C¡]¦³Ãö©ó¤å¥óªº©Ê½è¡A½Ð¨£¤U¤@³¹ªº»¡©ú¡C¡^


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