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
Lines 2-3. Each time a new visitor gets to "http://www.myadrotator.com/adrotator.asp"
a new session will be open, and by using the session method, we will define
session("ad") value to 0. In case the visitor is not new, the
value for session("ad") will exits, and that value will be keep.
Lines 6-10. The value for Session("ad") is increased by one
unless that value is 5 (which is the maximum value allowed in this script)
Lines 13-34. By using Select_case the page will return the information
necessary to display the appropiate ad at "mypage.html". The
first time a visitor accesses our page the ad display will be the first
one, the second time ad number two and so on. After 5 visits, the process
will start again. We may easily increase the number of ads just by adding
more obtions within Select_case and setting the maximum number from 5
to the new maximum in line 6.
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
Lines 2. We will need a text file with a number as its unique content.
The path to that file will be openned in variable "Wfile". Creating
this page within cgi-bin directory is a good way to prevent visitor from
accesing our counter (they will get an error when trying to access a ".txt
file within cgi-bin directory).
Lines 4-8. We will open our file, read the content to a variable ("hits"),
and in line 7 the variable will be increased by one.
Lines 10-12 . We will create a new file with the same name (overwriting
the previous file) with the content in "hits".
Line 14. The response page will be a javascript code containing the number
of hits.
|