PDA

View Full Version : Write "%>" as text in an asp page


Gary Williams
10-21-2005, 01:07 PM
Hi All,

I have written a simple vbs script that itself write a text file containing asp code. Here it is:
-----------------------------------
Dim oFSO
Dim oFile
Dim strLine
Dim strLoginFile
Dim strAgentRef

strAgentRef = 9997

strLoginFile = "d:\websites\test\www\dca\login" & strAgentRef & ".asp"

set oFSO = createobject ("scripting.filesystemobject")
set oFile = oFSO.createtextfile("" & strLoginFile & "",true)

strLine="<% response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%>"

oFile.writeline strLine

oFile.close
set oFile = nothing
set oFSO = nothing
--------------------------------------

This works fine and correctly produces the following code:

<% response.cookies("enabled")="1"
response.redirect("login99972.asp")%>

However, when I try to convert this script to an asp file, the " %> " causes a problem. I need to be able to tell the file that, in this case, this " %> " is to be treated as text only and not an end if statement.

I guess this is to do with those multiple single and double quotes again?

Regards

Gary

TheShaner
10-21-2005, 03:59 PM
You should escape the % sign. In ASP, escaping characters is a matter of doubling them, like you did with your quotes. So change your code to:

strLine="<%% response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%%>"
And let me know if that works for you. If it doesn't, try:

strLine="<<%% response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%%>>"

Hopefully one of the two will work. I've never tried it before, hehe.

-Shane

Gary Williams
10-21-2005, 04:45 PM
Hi TheShaner,

I'll give then a try now. I think I finally understand what is meant by "escape the character". I means nullify its effect in the code, yes?

Regards

Gary

Gary Williams
10-21-2005, 05:10 PM
Hi TheShaner,

This is the best I got. Using .....

strLine="<%% response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%%>"

I got as far as:

strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%
-------------------------------------------------------------------^

I can't get the final " > " to display.

Regards

Gary

TheShaner
10-21-2005, 05:14 PM
Yep. Escaping a character tells the compiler to treat the character as just that - a character - and not some special symbol that has meaning. So when your code does get interpretted, it doesn't get treated with any special attention. When your ASP starts running your code, it sees the <% and instantly thinks, "This indicates ASP code is inside." So doubling the % should instead say, "Ok, don't treat the % sign with any special attention. It's just a regular character in a string." Hope that helps your understanding of it. Let me know if it works for you.

EDIT: Just noticed your new post. Try doubling the > sign also. If that doesn't give you your desired result, do this instead. This should trick the ASP compiler since the < and % won't be next to each other:

strLine="<" & "%% response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")%%" & ">"
The above should definitely work :thumbsup: (crosses fingers!)

-Shane

Gary Williams
10-21-2005, 06:40 PM
Hi TheShaner,

Fixed it!!!

Here is the code that works:

strLine="<%"
strLine=strLine & vbCrLf
strLine=strLine & "response.cookies(""enabled"")=""1"" "
strLine=strLine & vbCrLf
strLine=strLine & "response.redirect(""login" & stragentref & "2.asp"")"
strLine=strLine & vbCrLf
strLine=strLine & "%" & ">"

along with the rest of the filesystemobject code, correctly produces the following when the asp file is accessed:

<%
response.cookies("enabled")="1"
response.redirect("login99972.asp")
%>

Success, hooray!

Regards

Gary