问题描述
我使用的是基本的 ASP,我想根据页面的 URL 显示两个图像之一。这是我最好的尝试:
<%
Dim myURL
myURL = "https://www.domainname.com:443/Index.asp"
If myURL = "<% response.write(curPageURL()) %>" Then
response.write("<img src="img/image1-logo-150x50.png" width="150" height="50" alt="sitename">")
Else
response.write("<img src="img/image2-logo-80x80.jpg" width="80" height="80" alt="sitename">")
End If
%>
我被卡住了,我的努力只能得到服务器错误 500 响应。任何见解将不胜感激。顺便说一句,我测试了 <% response.write(curPageURL()) %>
,它确实以我的代码中显示的格式检索页面 URL。
基于 GSerg 的链接,我将代码更改为此,但它仍然不起作用:
<%
Dim myURL
myURL = "https://www.domainname.com:443/Index.asp"
If myURL = "<% response.write(curPageURL()) %>" Then
response.write("<img src=""img/image1-logo-150x50.png"" width=""150"" height=""50"" alt=""sitename"">")
Else
response.write("<img src=""img/image2-logo-80x80.jpg"" width=""80"" height=""80"" alt=""sitename"">")
End If
%>
也许我误解了。
我已经使用 response.write 纠正了错误,服务器现在运行代码;但是,条件不起作用。我对页面 URL 进行了写入测试以获取 curPageURL() 返回的格式,但是当我在应该显示 image1 的页面上时,它只显示 image2。这是我正在使用的:
<%
Dim myURL
myURL = "curPageURL()"
If myURL = "https://www.domainname.com:443/Index.asp" Then
response.write("<img src=""img/image1-logo-150x50.png"" width=""150"" height=""50"" alt=""name"">")
Else
response.write("<img src=""img/image2-logo-80x80.jpg"" width=""80"" height=""80"" alt=""name"">")
End If
%>
解决方法
这个:
<%
Dim myURL
myURL = "curPageURL()" ' <-- Here's your problem
' [...]
%>
应该是这样的:
-
myURL = curPageURL()
调用curPageURL()
函数。- 您的旧代码
myURL = "curPageURL()"
使用了字符串文字"curPageURL()"
,这不是您想要的。
- 您的旧代码
- 我使用不区分大小写的字符串比较
StrComp(,vbTextCompare)
以确保稳健性,请参阅 https://devblogs.microsoft.com/scripting/how-can-i-compare-two-string-values-regardless-of-letter-case/ - 因为
Response.Write
是Sub
,所以在调用它时不需要()
。
<%
Dim myURL
myURL = curPageURL()
Const testUri = "https://www.domainname.com:443/Index.asp"
If StrComp( myURL,testUri,vbTextCompare ) = 0 Then
Response.Write "<img src=""img/image1-logo-150x50.png"" width=""150"" height=""50"" alt=""name"" />"
Else
Response.Write "<img src=""img/image2-logo-80x80.jpg"" width=""80"" height=""80"" alt=""name"" />"
End If
%>