PDA

View Full Version : Inserting HTML in ASP code


SteveH
03-18-2010, 11:05 AM
Hello

The following code displays a counter number in Times Roman (10pt/12pt).

Where would I insert HTML in the code to format the diaplay, please (border, font face, etc)?

<%@ Language = VBSCRIPT %>
<% Option Explicit %>
<%
' Variable Section
Const ForReading = 1
Const ForWriting = 2
Const UseText = 0 ' Use this constant if you want to use text for the counter display
Const UseImages = 1 ' Use this constant if you want to use images for the counter display
Dim objCounterFile, objFSO, strFilePath, strImageName, displayOption, imageDirectory
Dim strCounterNumber, iCounterNumber, strCurrentIP, strLastIP

' Set this equal either to 0 or 1, make it equal either UseImages of UseText
' depending on which you want to use to show your counter.

displayOption = 0

' Set this string equal to the sub-directories that you follow to your images directory
' This is from the directory that contains the file that executes "counter.asp"

imageDirectory = "images/"

'
' Gathering information
strCurrentIP = Request.ServerVariables("REMOTE_ADDR")
strFilePath = Server.MapPath("counter.txt")
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'
' Checking for the file counter.txt
If (objFSO.FileExists(strFilePath)) then
Set objCounterFile = objFSO.OpenTextFile(strFilePath, ForReading)
' Reading the information in from the text file
iCounterNumber = Cdbl(objCounterFile.ReadLine) + 1
strLastIP = Cstr(objCounterFile.ReadLine)
objCounterFile.Close
Else
' If the doesn't exist start counting at 1
iCounterNumber = 1
End If
'
' If the current visitor's IP is different from the last visitor's IP then log the new information
If (strLastIP <> strCurrentIP) then
Set objCounterFile = objFSO.CreateTextFile(strFilePath, True)
objCounterFile.WriteLine(iCounterNumber)
objCounterFile.WriteLine(strCurrentIP)
objCounterFile.Close
Else
iCounterNumber = iCounterNumber - 1
End If
'
' Destroying the Objects
Set objCounterFile = Nothing
Set objFSO = Nothing
'
' Setting up the number to be displayed
' This setup allows for later changing to images from text
strCounterNumber = CStr(iCounterNumber)
While (Len(strCounterNumber) > 0)
strImageName = Left(strCounterNumber, 1)
If (displayOption = UseText) then
Response.Write (strImageName)
Elseif (displayOption = UseImages) then
Response.Write ("<IMG SRC=""" & imageDirectory & strImageName & ".gif"" BORDER=0>")
End If
strCounterNumber = Mid(strCounterNumber, 2)
Wend
%>


I was looking for something along the lines of 'Response.Write(strCounterText)' in order to format it, but that does not appear in the script above.

Thanks

Steve

Old Pedant
03-18-2010, 07:10 PM
That's because the code is using an IMAGE for the counter output:

Response.Write ("<IMG SRC=""" & imageDirectory & strImageName & ".gif"" BORDER=0>")

Apparently your system has a series of files named
images/0.gif
images/1.gif
...
images/9.gif
and it displays the counter using those images. If you want a different appearance, you would need to use something like Photoshop to edit the image files.

There is, however, no reason to use those image files if your boss says it's okay to just use HTML instead.

You could replace all this code:

'
' Setting up the number to be displayed
' This setup allows for later changing to images from text
strCounterNumber = CStr(iCounterNumber)
While (Len(strCounterNumber) > 0)
strImageName = Left(strCounterNumber, 1)
If (displayOption = UseText) then
Response.Write (strImageName)
Elseif (displayOption = UseImages) then
Response.Write ("<IMG SRC=""" & imageDirectory & strImageName & ".gif"" BORDER=0>")
End If
strCounterNumber = Mid(strCounterNumber, 2)
Wend

With the much much simpler code:

...
%>
<span style="font-family: arial; font-size: 16pt; font-weight: bold; color: #333399;"><%=iCounterNumber%></span>
<%
...

The style there is just an example. Use whatever HTML style(s) you want.

SteveH
03-19-2010, 10:52 AM
Hello Old Pedant

Well that is the strange thing, because there are no image files or image folder containing images/0.gif, images/1.gif.

Anyway, not to worry - your snippet was just what I was looking for - something I can manipulate simply.

Cheers again, OP!

Steve

Old Pedant
03-19-2010, 07:05 PM
Yeah, I missed the fact that the "IF" in there would instead write out just the raw numbers one at a time:
If (displayOption = UseText) then
Response.Write (strImageName)
Elseif (displayOption = UseImages) then
Response.Write ("<IMG SRC=""" & imageDirectory & strImageName & ".gif"" BORDER=0>")
End If

But that's a braindead way to do it, anyway. Writing a string one character at a time. My way is much cleaner.

SteveH
03-20-2010, 03:35 PM
Hello OP

Yes, yours is a lot better.

You can see it at work here in the top left hand corner:

http://www.proofreading4students.com/

Many thanks again for all your help.

Steve