zl程序教程

您现在的位置是:首页 >  其他

当前栏目

javascriptasp教程第十三课--include文件

文件教程 include javascriptasp 第十三
2023-06-13 09:13:53 时间

ServerSideIncludes:

ExperiencedJavaScriptprogrammersknowthatcodereuseisgood.ExperiencedJavaScriptprogrammersalsoknowthatJavaScriptfunctionsaredatatypes.

So,weshouldbeabletostoreaJavaScriptfunctioninsideaSessionVariableoranApplicationVariable,right?Unfortunately,no.ThewaytoreuseJavaScriptfunctionsacrossmanypagesistouseSSI:ServerSideIncludes.

<%@LANGUAGE="JavaScript"%>
<HTML>
<HEAD>
<!--#includefile="script13a.asp"-->
<TITLE><%Response.Write(whatTimeIsIt())%></TITLE>
</HEAD>
<BODY>
<%
Response.Write("Thedateandtimeare"+DateTime+"<BR><BR>\r")
Response.Write("Tomorrow"sdateis"+Tomorrow+"<BR><BR>\r")
Response.Write("Tomorrowwillbea"+findDayOfWeek(Tomorrow)+"\r")
%>
</BODY>
</HTML>

ClickHeretorunthescriptinanewwindow.

Lookatthecodeforscript13.asp.Itcallsforanincludefileviathisline:<!--#includefile="script13a.asp"-->I"llletyouseetheincludefileinamoment.Butfirst,Ireprintedtheclient-sidecodedirectlybelow.

<HTML>
<HEAD>

<TITLE>10:57:20AM</TITLE>
</HEAD>
<BODY>
Thedateandtimeare4/11/200310:57:20AM<BR><BR>
Tomorrow"sdateis4/12/2003<BR><BR>
TomorrowwillbeaSaturday

</BODY>
</HTML>

ThefinalHTMLcodelookssoniceandsimple.You"dneverknowthattheDate()objecthadbeentorndownandputbacktogether.Belowistheincludefile.

<%
functionwhatTimeIsIt()
	{
	varm=newDate()
	varminute=m.getMinutes()
	varsecond=m.getSeconds()
	varampm=false

	if(minute>=0&&minute<10)
		{
		minute=("0"+minute)
		}
	if(second>=0&&second<10)
		{
		second=("0"+second)
		}
	varhours=m.getHours()
	if(hours>12)
		{
		ampm=true
		hours=hours-12
		}
	if(hours==12)
		{
		ampm=true
		}
	if(hours==0)
		{
		hours=hours+12
		ampm=false
		}
	if(ampm)
		{
		ampm="pm"
		}
	else
		{
		ampm="am"
		}
	varmyTime=hours+":"+minute+":"+second+ampm
	returnmyTime;
	}
varDateTime=newDate();
varMonth=(DateTime.getMonth()+1)+"/";
varDay=DateTime.getDate()+"/";
varYear=DateTime.getFullYear();
varDateTime=Month+Day+Year+""+whatTimeIsIt();

varTomorrow=newDate()
Tomorrow.setDate(Tomorrow.getDate()+1)
Month=(Tomorrow.getMonth()+1)+"/"
Day=Tomorrow.getDate()+"/"
Year=Tomorrow.getFullYear()
Tomorrow=Month+Day+Year

functionfindDayOfWeek(DateInQuestion)
	{

	//formatforDateInQuestionismm/dd/yyyyorm/d/yyyy
	//andpresumesthe/"sarepresent.	

	myRegExp=/\d{1,2}\//
	myMonth=(parseInt(DateInQuestion.match(myRegExp))-1)

	myRegExp=/\/\d{1,2}\//
	myDay=newString(DateInQuestion.match(myRegExp))
	myDay=parseInt(myDay.substring(1,myDay.length))

	myRegExp=/\/\d{4}/
	myYear=newString(DateInQuestion.match(myRegExp))
	myYear=parseInt(myYear.substring(1,myYear.length))

	DateInQuestion=newDate(myYear,myMonth,myDay)
	
	DayOfWeek=newArray
	DayOfWeek[0]="Sunday"
	DayOfWeek[1]="Monday"
	DayOfWeek[2]="Tuesday"
	DayOfWeek[3]="Wednesday"
	DayOfWeek[4]="Thursday"
	DayOfWeek[5]="Friday"
	DayOfWeek[6]="Saturday"
	DayOfWeek=DayOfWeek[DateInQuestion.getDay()]

	returnDayOfWeek;
	}
%>

Imaginethatyouhad50pagesthatallneedwhatTimeIsIt(),findDayOfWeek(),DateTime,andTomorrow.Youwouldn"twant50differentcopiesofthesefunctionsandvariables.No,youwouldratherhaveasinglecopyoftheseitemssothatyoucouldmanipulatethesinglecopyandexecuteyourupdatestoall50pagesatonce.

Nowyouknowhowtodothat.

MovingForward:

ThisconcludesSection03.NextupServerandErrorobjectsinSection04.