Active Server Pages: ASP and Javascript together


In this tutorial we will create a regular HTML page with a small javascript code, and we will use this javascript code to include in the page new information from a ".asp" file.

First, let´s check this two pages:
 
 

javascript.html
<html>
<title>My page</title>
<body>

<script language="javascript" src="javascript.asp"></script>

</body>
</html>

1
2
3
4
5
6
7
8
javascript.asp
document.write ("hello") 1

In the first file (javascript.html) we have include a javascript code in red, and within the code the file name from which we will get information to complete our page (src: source). So that we are asking for information to complete our page (javascript.html) to a different page (javascript.asp). This time we have only include the file name, but we may use the complete url even from a different site (for example: http://www.adifferentsite.com/javascript.asp).

In the second file (javascript.asp) we have include the information necessary to write in the document the word "hello". This time we have use ".asp" extensión for the second page even though other extensions are possible.

The resulting page in our example will be like this one:
 

javascript.html (resulting page)
hello

 

So we already know we are able to include a text generated in one page within a different one. As the information we are including can be generate within a ".asp" file, we can add information dinamically by using Active Server Pages.

Let´s try two examples:

    A very rudimentary banner rotator system
    A simple text hit counter

In case you are using ".asp" files whithin a site and the origen of the information we want to include in the page is originated in the same site, using any of those  system is not very convenient: the number of conections to the server will increase, and there may be an important delay.  In case you want to include information obtained from an asp script in several pages but you do not want to copy the asp script  in all of them, you may use the Include instruction, so that the script is only in one page, and by changing it you will change the results in several pages.

A very rudimentary banner rotator system

Our page is "mypage.html" and we are requesting information to complete it from "http://www.myadrotator.com/adrotator.asp" which may be located in the same or in a different site. The information provided by the second file will determinate the ad to be display in our page.
 

mypage.html
<html>
<title>My page</title>
<body>

<script language="javascript" src="http://www.myadrotator.com/adrotator.asp"></script>

</body>
</html>

1
2
3
4
5
6
7
8
adrotator.asp
<% 
if session("ad")="" then 
     session("ad")=0
end if

if session("ad")=5 then 
   session("ad")=1 
else 
    session("ad")=session("ad") 1 
end if 
%> 

<% Select Case session("ad") %>
<% case 1 %>
     document.write ("<A HREF=http://www.1site.com">)
     document.write ("<IMG SCR=1.gif>")
     document.write ("</A>")
 <% case 2 %>
     document.write ("<A HREF=http://www.2site.com">)
     document.write ("<IMG SCR=2.gif>")
     document.write ("</A>")
<% case 3 %>
     document.write ("<A HREF=http://www.3site.com">)
     document.write ("<IMG SCR=3.gif>")
     document.write ("</A>")
<% case 4 %>
     document.write ("<A HREF=http://www.4site.com">)
     document.write ("<IMG SCR=4.gif>")
     document.write ("</A>")
<% case 5 %>
     document.write ("<A HREF=http://www.4site.com">)
     document.write ("<IMG SCR=4.gif>")
     document.write ("</A>")
<% End select %>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
NOTE: when writing the javascript code you must be very carefull: do not include brakets within the brakets in the javascript code
    p.e.:    Correct: document.write ("<A HREF=http://www.4site.com">)
                Incorrect: document.write ("<A HREF="http://www.4site.com"">)
                Correct: document.write ("<A HREF='http://www.4site.com'">)
    You may include in the response as many lines like the ones above or any other javascript code.

A simple text hit counter

Our page is "mypage.html" and we are requesting information to complete it from "http://www.mycounter.com/hitcounter.asp" which may be located in the same or in a different site. The information provided by the second file will allow to get a text with the number of hits in our page.
 
 

mypage.html
<html>
<title>My page</title>
<body>

<script language="javascript" src="http://www.mycounter.com/hitcounter.asp"></script>

</body>
</html>

1
2
3
4
5
6
7
8
hitcounter.asp
<% 
Wfile="c:\mydir\cgi-bin\hitcounter.txt"

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(Wfile)
hits = Clng(a.ReadLine)
hits = hits 1
a.close

Set a = fs.CreateTextFile(Wfile,True)
a.WriteLine(hits)
a.Close
%>
document. write ("Number of hits since 2002/01/01: <% =hits %>")

1
2
3
4
5
6
7
8
9
10
11
12
13
14

 

Tutorial home page