javascript asp教程第十三课--include文件

Server Side Includes:

Experienced JavaScript programmers know that code reuse is good. Experienced JavaScript programmers also know that JavaScript functions are data types.

So, we should be able to store a JavaScript function inside a Session Variable or an Application Variable, right? Unfortunately, no. The way to reuse JavaScript functions across many pages is to use SSI: Server Side Includes.

<%@LANGUAGE="JavaScript"%> <HTML> <HEAD> <!--#include file="script13a.asp"--> <TITLE><% Response.Write( whatTimeIsIt() ) %> </TITLE> </HEAD> <BODY> <% Response.Write("The date and time are " + DateTime + "<BR><BR>\r") Response.Write("Tomorrow's date is " + Tomorrow + "<BR><BR>\r") Response.Write("Tomorrow will be a " + findDayOfWeek(Tomorrow) + "\r") %> </BODY> </HTML>

Click Here to run the script in a new window.

Look at the code for script13.asp. It calls for an include file via this line: <!--#include file="script13a.asp"--> I'll let you see the include file in a moment. But first, I reprinted the client-side code directly below.

<HTML> <HEAD> <TITLE>10:57:20 AM </TITLE> </HEAD> <BODY> The date and time are 4/11/2003 10:57:20 AM<BR><BR> Tomorrow's date is 4/12/2003<BR><BR> Tomorrow will be a Saturday </BODY> </HTML>

The final HTML code looks so nice and simple. You'd never know that the Date() object had been torn down and put back together. Below is the include file.

<% function whatTimeIsIt() { var m=new Date() var minute=m.getMinutes() var second=m.getSeconds() var ampm=false if (minute >=0 && minute < 10) { minute=("0" + minute) } if (second >= 0 && second < 10 ) { second=("0" + second) } var hours=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" } var myTime=hours + ":" + minute + ":" + second + ampm return myTime; } var DateTime = new Date(); var Month = (DateTime.getMonth() + 1) + "/"; var Day = DateTime.getDate() + "/"; var Year = DateTime.getFullYear(); var DateTime = Month + Day + Year + " " + whatTimeIsIt(); var Tomorrow=new Date() Tomorrow.setDate(Tomorrow.getDate() + 1) Month = (Tomorrow.getMonth() + 1) + "/" Day = Tomorrow.getDate() + "/" Year = Tomorrow.getFullYear() Tomorrow = Month + Day + Year function findDayOfWeek(DateInQuestion) { // format for DateInQuestion is mm/dd/yyyy or m/d/yyyy // and presumes the /'s are present. myRegExp=/\d{1,2}\// myMonth=(parseInt(DateInQuestion.match(myRegExp)) -1) myRegExp=/\/\d{1,2}\// myDay=new String(DateInQuestion.match(myRegExp)) myDay=parseInt( myDay.substring(1,myDay.length) ) myRegExp=/\/\d{4}/ myYear=new String(DateInQuestion.match(myRegExp)) myYear=parseInt( myYear.substring(1,myYear.length) ) DateInQuestion=new Date(myYear,myMonth,myDay) DayOfWeek=new Array 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()] return DayOfWeek; } %>

Imagine that you had 50 pages that all need whatTimeIsIt(), findDayOfWeek(), DateTime, and Tomorrow. You wouldn't want 50 different copies of these functions and variables. No, you would rather have a single copy of these items so that you could manipulate the single copy and execute your updates to all 50 pages at once.

Now you know how to do that.

Moving Forward:

This concludes Section 03. Next up Server and Error objects in Section 04.

时间: 2024-09-20 05:53:48

javascript asp教程第十三课--include文件的相关文章

javascript asp教程第十三课--include文件_ASP基础

Server Side Includes: Experienced JavaScript programmers know that code reuse is good. Experienced JavaScript programmers also know that JavaScript functions are data types. So, we should be able to store a JavaScript function inside a Session Variab

javascript asp教程第五课--合二为一_ASP基础

两条防线,一个函数: 试问你如何能保证客户端和服务器端具有相同的功能?表单域的验证闪现在我们眼前.别人把你的html复制到另外一个脚本,然后改变客户端的表单域验证--这并不是一件难事.摆在眼前的解决方法是将表单域的验证放置在服务器端.但那又意味着因为使用者的一个小错误,都要给服务器端要返回一串的错误信息.那么,我们何不同时拥有二者呢?不仅仅如此,我们还可以在客户端和服务器端使用同一个javascript函数来保证二者的完全一致性. 看看下面这一小段,请特别注意一下checkMyZip()函数.

javascript asp教程第六课-- response方法_ASP基础

response 对象:reponse是asp中六个对象之一.它代表了服务器端对浏览器的回应.response有8种方法,9种属性和一个集.在这一课,我们就重点讲述方法.方法:在javascript中,asp方法使用括号.请注意依赖response.buffer的两个方法,我们将在下一课讲到他们.同样应该注意到addheader()和redirect(),因为他们必须优先于write()执行.所有的方法都在上面描述和演示了.下面我将详细讲述每一个方法.我将花点额外的时间来讲述我们用的最多的两个方

javascript asp教程第六课-- response方法

response 对象: reponse是asp中六个对象之一.它代表了服务器端对浏览器的回应.response有8种方法,9种属性和一个集.在这一课,我们就重点讲述方法. 方法: 在javascript中,asp方法使用括号.请注意依赖response.buffer的两个方法,我们将在下一课讲到他们.同样应该注意到addheader()和redirect(),因为他们必须优先于write()执行. 所有的方法都在上面描述和演示了.下面我将详细讲述每一个方法.我将花点额外的时间来讲述我们用的最多

javascript asp教程第五课--合二为一

两条防线,一个函数: 试问你如何能保证客户端和服务器端具有相同的功能?表单域的验证闪现在我们眼前.别人把你的html复制到另外一个脚本,然后改变客户端的表单域验证--这并不是一件难事.摆在眼前的解决方法是将表单域的验证放置在服务器端.但那又意味着因为使用者的一个小错误,都要给服务器端要返回一串的错误信息.那么,我们何不同时拥有二者呢?不仅仅如此,我们还可以在客户端和服务器端使用同一个javascript函数来保证二者的完全一致性. 看看下面这一小段,请特别注意一下checkMyZip()函数.

javascript asp教程第三课 new String() 构造器

开始: new String() 是本课程计划中较早出现的另一个让人感觉到奇怪的地方.但和转义字符一样, new String()是创建一个成功的asp javascript应用的必须元素.下面是本课的两个脚本: 下面是实际上承担重量的脚本: 行为中的new String( ): 现在我们来看看下面的asp行. Request.Form 我们将在后面有独立的课程来讲授.下面才是我们现在要讲的重点.在request.form中所持有的数据(来自用户的数据)并不是一个javascript数据类型.相

javascript asp教程第十一课--Application 对象_ASP基础

Overview: The Application Object represents a collection of ASP pages. The Application object has zero (0) properties, two (2) collections, two (2) methods, and two (2) events. Get Started: Below are a couple scripts for lesson11. <%@LANGUAGE="Jav

javascript asp教程第八课--request对象_ASP基础

Request Object: Request has five (5) Collections, one (1) Property, and one (1) Method. You'll use the Collections far more than the property or the method. Request Collections: Below is a table of the Request Collections and descriptions of how they

javascript asp教程第七课--response属性_ASP基础

Below is a table of Response Properties along with examples and explanations. Response Properties Buffer Response.Buffer = trueAllows for the buffering of outputCacheControl Response.CacheControl="Public" Sets Cache to "Public" or &quo