欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-04 18:32:10
server side includes: experienced javascript programmers know that code reuse is goo...

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.