【问题标题】:Using ASP, how do you use if/else to display one of two images?使用 ASP,如何使用 if/else 显示两个图像之一?
【发布时间】:2021-06-03 16:14:55
【问题描述】:

我正在使用基本的 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 响应。任何见解将不胜感激。顺便说一句,我测试了&lt;% response.write(curPageURL()) %&gt;,它确实以我的代码中显示的格式检索了页面 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
  %>

【问题讨论】:

  • 这能回答你的问题吗? About using Double quotes in Vbscript
  • 我从链接中得到的结论是,我必须在每个内部双引号集周围添加第二组双引号。我试过了,但没有用。
  • If myURL = "&lt;% response.write(curPageURL()) %&gt;" Then 没有意义。你的意思是If myURL = curPageURL() Then?
  • @GSerg 所说的是绝对正确的,你没有误解,但你也有If myURL = "&lt;% response.write(curPageURL()) %&gt;" Then 的问题,它永远不会工作。你不能在这样的If 语句中使用Response.Write(),它永远不会起作用。相反,您需要将myURL 字符串变量与我假设的包含当前页面curPageURL 的字符串进行比较。
  • 是的,GSerg,谢谢。我应该使用 If myURL = curPageURL() Then

标签: vbscript asp-classic


【解决方案1】:

这个:

<%
    Dim myURL
    myURL = "curPageURL()" ' <-- Here's your problem
  
    ' [...]
%>

应该像下面这样:

<%
    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
%>

【讨论】:

  • 谢谢你,戴。您的解决方案完美运行。
  • @WarrenRosenfeld 出于好奇,您为什么在 2021 年使用 Classic ASP?它已经过时了 20(是的,20)年......
  • 欢迎来到 StackOverflow。如果贡献者解决了您的问题 - 将答案标记为已接受 :) @Dai - 只要当前版本的 IIS 支持经典 ASP,它们就不会过时。那里仍然有惊人的数量。开发人员通常会在现有代码的基础上进行构建,而不是丢弃所有内容并重新开始。一些开发人员喜欢它。实际上有一个用经典 ASP 编写的现代框架 - asplite.com
  • @John 这个家伙谈到了使用 Access 数据库以及 w3schools 是一个很好的资源,抱歉不能把他当回事。
  • @WilliamK 我不同意。我刚刚检查了 W3Schools.com,虽然我同意该网站在视觉上不那么混乱(我什至不知道我禁用了 AdBlock!)仍然提供了很多可怕的建议,例如 this page on PHP 呈现未经处理的用户输入直接使用 &lt;?php echo $_POST["name"]; ?&gt; 而不是 safely 使用 &lt;?= htmlentities( $_POST["name"] ) ?&gt; - 页面确实有一条亮黄色警告消息,但它是关于数据验证,而不是安全地呈现用户提供的数据:它没有提到完全没有 XSS。
猜你喜欢
  • 1970-01-01
  • 2018-10-04
  • 2013-07-09
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 2023-02-09
相关资源
最近更新 更多