20-3 ���νd�Ҥ@�G�������ӳX����

¥»¸`±N¨Ï¥Î¨â­Ó¤p½d¨Ò¨Ó»¡©ú¦p¦ó¨Ï¥Î JavaScript ¶i¦æÀÉ®×Ū¨ú»P¥Ø¿ýŪ¨úªºÀ³¥Î¡C

­º¥ý§Ú­Ì¥ý¬Ý¬Ý­p¼Æ¾¹ªº½d¨Ò¡C§Ú­Ì¦b«e­±´X­Ó¤p¸`¤]´¿¸g¤¶²Ð¹L­p¼Æºô­¶ªº½d¨Ò¡A¨º¨Ç½d¨Ò¬O§Q¥Î Application ©M Session ÅܼƨӹF¨ì­p¼Æªº¥\¯à¡A¥Ñ©ó³o¨ÇÅܼƳ£Àx¦s¦b°O¾ÐÅ餤¡A¦]¦¹³o¨Ç­p¼Æ¸ê®Æ·|¦]¦øªA¾¹ªº­«¶}¾÷¦Ó¬y¥¢¡C¦Ó¥»¤p¸`¤¤ªº­p¼Æºô­¶¡A¬O±N­p¼Æ¸ê®ÆÀx¦s¦b¤@­Ó­p¼ÆÀɮפ§¤¤¡A¤ñ¸ûí©w¡A½Ð¨£¦¹½d¨Ò¡G

Example¡]fileAccess/pageHitCounter01.asp¡^¡G

¦b¤W­z½d¨Ò¤¤¡A¥u­n¨Ï¥ÎªÌ«ö¤UÂsÄý¾¹ªº¡u­«·s¾ã²z¡v¡A­p¼Æ¾¹´N·|¥[ 1¡C¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/pageHitCounter01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¥HÀɮ׬°¥Dªº­p¼Æ¾¹" %>
<!--#include file="../head.inc"-->
<hr>

<%  
fso = new ActiveXObject("Scripting.FileSystemObject");
countFile = Server.MapPath("pageHitCounter01.cnt");	// §ä¥X­p¼ÆÀɮצbµwºÐ¤¤ªº¹ê»Ú¦ì¸m
fid = fso.OpenTextFile(countFile, 1);			// ¶}±Ò°ßŪÀÉ®×
count = fid.ReadLine();					// ±qÀÉ®×Ū¥X­p¼Æ¸ê®Æ
fid.Close();						// Ãö³¬ÀÉ®×
count++;						// ¼W¥[­p¼Æ¸ê®Æ
fid = fso.OpenTextFile(countFile, 2);			// ¶}±ÒÀɮרä¹³\¼g¤J
fid.WriteLine(count);					// ¼g¤JÀÉ®×
fid.Close();						// Ãö³¬ÀÉ®×
%>

<center>
±z¬O¥»­¶ªº²Ä <font color=green><%=count%></font> ¦ì³X«È.¡I
<p>¡]«ö¡u<a href="javascript:history.go(0)">­«·s¾ã²z</a>¡v¥H¼W¥[­p¼Æ¸ê®Æ¡C¡^
</center>

<hr>
<!--#include file="../foot.inc"-->

¥Ñ¤W­zµ{¦¡½X¥i¬Ý¥X¡A§Ú­Ì¥²¶·¥ý²£¥Í¤@­Ó FileObject ªºª«¥ó¡A¦A¸g¥Ñ¦¹ª«¥óªº OpenTextFile ¤èªk¨Ó¶}±Ò­p¼ÆÀɮרæ^¶ÇÀɮ׫ü¼Ð Out¡AµM«á¦A¸g¥Ñ Out ªº ReadLine ©M WriteLine ¤èªk¨Ó¹ïÀɮ׶i¦æŪ¨ú©M¼g¤J¡C¦ý¦b¨Ï¥Î¦¹­p¼Æºô­¶¤§«e¡A§Ú­Ì¥²¶·¥ý·Ç³Æ¤@­Ó­p¼ÆÀɮס]¦b¦¹¨Ò¬O pageHitCounter.cnt¡^¡A¥H«KÀx¦s­p¼Æ¸ê®Æ¡C¥Ñ©ó­p¼Æ¸ê®Æ«ÇÀx¦s¦bÀɮפ§¤¤¡A¦]¦¹¨Ã¤£·|¦]¬°¦øªA¾¹ªº­«¶}¾÷¦Ó³y¦¨­p¼Æ¸ê®Æªº¬y¥¢¡C

§Ú­Ì¤]¥i¥H±N­p¼Æ¥\¯à¼g¦¨¤@­Ó¨ç¼Æ¡A¨ÃÅýµ{¦¡½X¦Û°Ê¥h´M§ä­p¼ÆÀɮסA³o®É­Ô¥u­nºô­¶¾É¤J¥]§t¦¹¨ç¼ÆªºÀɮסA´N¥i¥H¨ã³Æ­p¼Æ¥\¯à¡C¨Ò¦p¡G

Example¡]fileAccess/pageHitCounter02.asp¡^¡G

¦b¤W­z½d¨Ò¤¤¡A¥u­n¨Ï¥ÎªÌ«ö¤UÂsÄý¾¹ªº¡u­«·s¾ã²z¡v¡A­p¼Æ¾¹´N·|¥[ 1¡C¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/pageHitCounter02.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<%@language=JScript%>
<% title="¥HÀɮ׬°¥Dªº­p¼Æ¾¹" %>
<!--#include file="../head.inc"-->
<hr>

<center>
<!--#include file="counter.inc"-->
±z¬O¥»­¶ªº²Ä <font color=green><%=pageHitCounter()%></font> ¦ì³X«È.¡I
<%counterFile=Request.ServerVariables("URL") + ".cnt";%>
<p>¡]¥»­¶ªº­p¼Æ¸ê®ÆÀx¦s¦b <a href="<%=counterFile%>"><%=counterFile%></a>¡C¡^
<p>¡]«ö¡u<a href="javascript:history.go(0)">­«·s¾ã²z</a>¡v¥H¼W¥[­p¼Æ¸ê®Æ¡C¡^
</center>

<hr>
<!--#include file="../foot.inc"-->

¬ö¿ý­p¼Æ¸ê®ÆªºÀɮ׫h¬O¦b

­ì©lÀÉ¡]fileAccess/pageHitCounter02.asp.cnt¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
17061

¦Ó counter.inc ªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]fileAccess/counter.inc¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<script runat=server language=jscript>
function pageHitCounter(){
	fso = new ActiveXObject("Scripting.FileSystemObject");
	counterFile = Request.ServerVariables("PATH_TRANSLATED") + ".cnt";	// §ä¥X°O¼ÆÀɮצbµwºÐ¤¤ªº¹ê»Ú¦ì¸m
	fid = fso.OpenTextFile(counterFile, 1);			// ¶}±Ò°ßŪÀÉ®×
	count = fid.ReadLine();					// ±qÀÉ®×Ū¥X°O¼Æ¸ê®Æ
	fid.Close();						// Ãö³¬ÀÉ®×
	count++;						// ¼W¥[°O¼Æ¸ê®Æ
	fid = fso.OpenTextFile(counterFile, 2);			// ¶}±ÒÀɮרä¹³\¼g¤J
	fid.WriteLine(count);					// ¼g¤JÀÉ®×
	fid.Close();						// Ãö³¬ÀÉ®×
	return(count);
}
</script>

<script runat=server language=vbscript>
Function pageHitCounter
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	counterFile=Request.ServerVariables("PATH_TRANSLATED") & ".cnt"	' §ä¥X°O¼ÆÀɮצbµwºÐ¤¤ªº¹ê»Ú¦ì¸m
	'Response.Write(counterFile)
	Set Out= fso.OpenTextFile(counterFile, 1, FALSE, FALSE)	' ¶}±Ò°ßŪÀÉ®×
	count = Out.ReadLine					' ±qÀÉ®×Ū¥X°O¼Æ¸ê®Æ
	Out.Close						' Ãö³¬ÀÉ®×
	count= count+1						' ¼W¥[°O¼Æ¸ê®Æ
	Set Out= fso.CreateTextFile (counterFile, TRUE, FALSE)	' ¶}±ÒÀɮרä¹³\¼g¤J
	Out.WriteLine(count)					' ¼g¤JÀÉ®×
	Out.Close						' Ãö³¬ÀÉ®×
	pageHitCounter=count					' ¦^¶Ç¸ê®Æ
End Function
</script>

¦b¤W­z½d¨Ò¤¤¡A­p¼ÆÀɮתº¦WºÙ³£¬O­ìºô­¶ÀɮצWºÙ¦A¥[¤W ".cnt"¡A¦]¦¹¥u­n¦b­ìºô­¶¾É¤J count.inc¡A¦¹ºô­¶´N¨ã¦³­Ó§O­p¼Æ¥\¯à¡C¦¹¥~¡A¦¹ÀÉ®×¥]§t¨â­Ó¨ç¼Æ¡A¥i¥H¤À§O¥Î¦b JScript ©M VBScript¡C


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò