Jul 04 2002

Using ASP scripts from HTM pages

Posted by admin under ASP

1. What does he mean?
Well I guess that's what you must be asking yourself - and I really have to agree the title looks kind of cryptic. I understand I'd better motivate you to keep reading, so I do my best: Imagine you have this site and you have a Latest news-function where news headlines are read from a database. It might look like this:

 Latest news


You have some administrative pages to add headlines. This page ( the one that shows the headlines ) reads the headlines from the database and generates HTML code for it.





It works great showing those headlines from ASP pages - and on that specific site. Now, imagine I would like to show the same news on another site. I have access to ASP on this server as well, so maybe I should just copy the script and have the Access-database replicate itself to this server as well, maybe every night? That doesn't sounds too funny does it? Or even worse, let's say I had no ASP access on this site...

So, what I will show you in this article is a technique for calling ASP scripts ( on the same or a totally different server ) from within a HTM page.

2. The idea



I found out about this technique when developing AdMentor - the free ASP ad rotator. I havn't released the AdMentor version containing this code yet, but will very soon. But you will get sourcecode for the 'Latest news' function I discussed earlier. Lets just begin with looking at the ASP code and database layout we are starting with:

Database NEWS.MDB

Table NEWS id AutoNumber headline char(100) url char(255) datum DateTime

We also have a ASP function for reading the database and generating sourcecode. It is located in gennews.asp and looks something like this:

<%
Dim oConn, oRS, strRet
Dim strConnect
strConnect = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="& Server.MapPath("/articles/art1/news.mdb") &";DriverId=25;FIL=MS Access;MaxBufferSize=512;PageTimeout=5"
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open strConnect
Set oRS = oConn.Execute("select * from headlines")
strRet = "<table border=""0"" cellPadding=""5"" cellSpacing=""0"" width=""100%"">"
strRet = strRet & "<tr>"
strRet = strRet & "<td align=""left"" bgColor=""#000099""><font color=""white"" face=""verdana,arial,helvetica"""
strRet = strRet & " size=""-1""><nobr>&nbsp;<b>Latest news</b></nobr><br>"
strRet = strRet & "</font></td>"
strRet = strRet & "</tr>"
strRet = strRet & "<tr>"
strRet = strRet & "<td align=""left"" bgColor=""#eeeeaa"" vAlign=""top"" width=""100%""><ul>"
While Not oRS.EOF
strRet = strRet & "<li><a href=""" & oRS("url") & """><font size=""1"" face=""Verdana"">" & oRS("datum")
strRet = strRet & " " & oRS("headline") & "</font></a>"
strRet = strRet & "</li>"
oRS.MoveNext
Wend
strRet = strRet & "</ul>"
strRet = strRet & "</td>"
strRet = strRet & "</tr>"
strRet = strRet & "</table>"
Response.Write strRet
%>
3. What it looks like

On this page ( a normal ASP page and the database is located on this very same server ) I just include gennews.asp like this: From your ASP page we just include the file where we our litte news ticker to appear:

<!-- #INCLUDE FILE="gennews.asp" --> 




And it looks like:
 Latest news


Now, lets think what ASP actually does: It generates pure HTML code that is sent to the client. That's the whole idea about ASP and just what we want. But we want to call the gennews.asp script from within a usual HTM page. To work this out we are gonna use JavaScript.

I bet you are somewhat familiar with the JavaScript tag:
<script language="JavaScript"> HERE IS THE SCRIPT;</script>


Now, did you know you can specify where the source of the script is ( kind of like the src parameter in the IMG tag. )
<script src="pathtoscript"></script>


Now, lets look at the HTML code we should insert into a static HTM page as it should look like to work - and I explain it line by line later:
<!------- AdMentor Ad code ------------->
<script language="JavaScript"> var strCode = '';</script>
<script src="http://www.aspcode.net/articles/art1/gennews.asp?htm=1"></script>
<script language="JavaScript">document.write(strCode);</script>
<!--------- End AdMentor Ad code --------------->



This is what we do:
1. We declare a variable called strCode
2. We call our gennews.asp script
3. We write the strCode variable into the HTML document.


So, did you remember what the secret of ASP was? It creates HTML code. Our ASP script gennews.asp generates HTML code. Somehow we should get the variable strCode set to the generated HTML code and then it gets written into the HTM page! What we do is addthree lines of code into our gennews.asp code:
<%
'
'
'
strRet = strRet & "</table>"
' HERE WE ADD SOME NEW STUFF
If Request.QueryString("htm")="1" Then
strRet = "strCode='" & strRet & "'"
Response.Buffer = TRUE
Response.ContentType = "application/x-javascript"
Response.Write strRet
End If 'END OF NEW STUFF
Response.Write strRet
%>


In our HTM call from above ( script src-part ) did you notice the src="http://www.aspcode.net/articles/art1/gennews.asp?htm=1

We add this variable and check it from within the script. If it is set to 1 then the resulting code gets modified to strCode = ' thehtmlcode ' So, lets take on our script evaluator suit and excute our HTM code:
<!------- AdMentor Ad code ------------->
<script language="JavaScript"> var strCode = '';</script>
<script src="http://www.aspcode.net/articles/art1/gennews.asp?htm=1"></script>
<script language="JavaScript">document.write(strCode);</script>
<!--------- End AdMentor Ad code --------------->


1. Declare the variable strCode
2. When asking for the script located at gennews.asp that ASP code will be evaluated and the strCode variable will be set to its resulting HTML code.
3. We write the HTML into our document. VOILA!