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.

(0)

相关推荐

  • 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 Variab

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

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

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

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

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

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

  • javascript asp教程第十课--global asa

    Global.asa: First of all, what is a global.asa? It's an optional script file that holds certain "global" information that you can access through the entire ASP appliciation. The global.asa is a plain text file saved with the .asa extension. You

  • javascript asp教程第七课--response属性

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

  • javascript asp教程第四课 同时使用vbscript和javascript

    开始: 你能让javascript和vbscript实现从同一个表格里传出音乐.看看下面的脚本: function JSGreeting() { return "Greetings from a JavaScript Function"; } Function VBGreeting() VBGreeting="Greetings from a VBScript Function" End Function Function toDollars(x) toDollar

  • javascript asp教程第八课--request对象

    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教程第十一课--Application 对象

    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教程第二课--转义字符

    并非是题外话:我们在第二课中要讲到的"转义字符"看起来与我们的课程并没有关系.事实并非如此.转义字符是在 asp中使用javascript的一个完整部分.而第二课要讲的恰恰就是转义字符.没有转义字符,那么你为了 写出有效率的asp脚本将会遇到许多的困难.抱歉,下面进入正题:以下是第二课的asp脚本.还记不记得在第一课的列子中,我们怎样编写了一个html输出都在同一行的代码.转义字符允许我们增加 换行符号.下面是我们上边例子在客户端html输出页面的代码:换行符:我又在下面重新打了一行a

随机推荐