| 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>
|
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>
|
1
2 3 4 5 6 7 8 |
| adrotator.asp | |
| <%
if session("ad")="" then session("ad")=0 end if if session("ad")=5 then
<% Select Case session("ad") %>
|
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 |
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>
|
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.CreateTextFile(Wfile,True)
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |