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 is able to store a JavaScript function inside a sessions Variable or an application Variable, right? Unfortunately, No. The way to reuse JavaScript functions across many pages are to use Ssi:server Side.
<% @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'll Be a" + Finddayofweek (Tomo Rrow) + "\ r")
%>
</BODY>
</HTML>
Click here to run the script in a new window.
Look in the code for script13.asp. It calls for a include file via this line: <!--#include file= "script13a.asp"--> I ' ll have you E include file in a moment. But 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'll be a Saturday
</BODY>
</HTML>
The final HTML code looks and simple. You ' d never know this 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 < ten) {minute= ("0" + Minute)} if (second >= 0 && second < 1 0) {second= ("0" + second)} var hours=m.gethours () if (Hours >) {ampm=true} if ( hours==12) {ampm=true} if (hours = = 0) {hours=hours+12 ampm=false} if (ampm) {ampm= "PM"} Els
e {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.ge Tdate () + "/" 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" DayO
Fweek[4]= "Thursday" dayofweek[5]= "Friday" dayofweek[6]= "Saturday" Dayofweek=dayofweek[dateinquestion.getday ()]
return DayOfWeek;
}%>
Imagine that's you had pages of all need Whattimeisit (), Finddayofweek (), DateTime, and tomorrow. You are wouldn ' t want different copies of these functions and variables. No, you would rather have a copy of this items so this is could manipulate the single copy and execute your updat Es to all pages at once.
Now, know how.
Moving Forward:
This is concludes section 03. Next up Server and Error objects in section 04.