简体   繁体   English

使用来自 VBScript 经典 ASP 页面的 JavaScript alert()

[英]Using JavaScript alert() from VBScript Classic ASP Page

I'm working with a classic ASP VBScript page that queries post town details for a UK postcode given as a parameter, and I've been adding some additional code to catch error situations, eg:我正在使用一个经典的 ASP VBScript 页面,该页面查询作为参数给出的英国邮政编码的邮政城镇详细信息,并且我一直在添加一些额外的代码来捕获错误情况,例如:

  1. A blank postcode - I'd like to handle this by displaying an alert dialog.一个空白的邮政编码 - 我想通过显示一个警告对话框来处理这个问题。
  2. A postcode supplied without a space is handled by inserting the missing space.不带空格的邮政编码通过插入缺失的空格来处理。

We're also in the process of upgrading our website to Google Chrome and MS Edge, so we need to use JavaScript where possible for scripting - I'd like to tell the user that the postcode is blank using the alert() function and I was thinking that the best way to do this would be as follows:我们也正在将我们的网站升级到 Google Chrome 和 MS Edge,因此我们需要在可能的情况下使用 JavaScript 进行脚本编写 - 我想使用 alert() function 告诉用户邮政编码为空,我正在考虑这样做的最佳方法如下:

response.write("<SCRIPT language=" & chr(34) & "javascript" & chr(34) & ">alert('Please enter a postcode;');</SCRIPT>") response.write("<SCRIPT language=" & chr(34) & "javascript" & chr(34) & ">alert('请输入邮政编码;');</SCRIPT>")

When I try this, I get one of two outputs in the input text box on the page where the result is saved:当我尝试这个时,我在保存结果的页面上的输入文本框中得到了两个输出之一:

  1. An HTML-formatted error message like: Microsoft VBScript compilation error '800a0400' Expected statement line 14 HTML 格式的错误消息,例如:Microsoft VBScript 编译错误“800a0400”预期语句行 14
  2. The <SCRIPT> tag. <SCRIPT> 标签。

I've also tried using %> and %< tags around a <SCRIPT> tag, which is most likely what gives me the second output (I'm typing this on my personal computer and the testing environment is on my work computer).我也尝试过在 <SCRIPT> 标签周围使用 %> 和 %< 标签,这很可能是我得到第二个 output 的原因(我在我的个人电脑上输入这个,测试环境在我的工作电脑上)。

I've been looking at a couple of other questions on StackOverlow and Inte.net searches:我一直在查看关于 StackOverlow 和 Inte.net 搜索的其他几个问题:

Is there anything I've missed as I'd like to use alert() and the MsgBox() function doesn't work, which I found out when I tried it earlier.有什么我错过的,因为我想使用 alert() 和 MsgBox() function 不起作用,这是我在之前尝试时发现的。

Thanks @user692942 - that was definitely helpful: what I did in the end was to add the following to the ASP page:谢谢 @user692942 - 这绝对有帮助:我最后所做的是将以下内容添加到 ASP 页面:

if (strPostcode = "") then
%>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <HTML>
    <BODY>
      <SCRIPT type="text/javascript">
        alert("Please enter a postcode!");
      </SCRIPT>
    </BODY>
  </HTML>
<%
response.end
end if

And this is working as I wanted it to, so thank you for your guidance: :)这是我想要的,所以感谢您的指导::)

It entirely depends on how you have structured the ASP page, but personally, it is far easier to break out of the ASP code blocks to work with HTML instead of using Response.Write .这完全取决于您构建 ASP 页面的方式,但就个人而言,打破 ASP 代码块以使用 HTML 而不是使用Response.Write要容易得多。

<% @Language="VBScript" CodePage=65001 %>
<%
Response.Charset = "UTF-8"
Response.CodePage = 65001

'ASP Code here
%>
<!doctype html>
<html>
  <head>
    <title>Test Page</title>
  </head>
  <body>
<%
'ASP Code here
%>
    <script type="text/javascript">
      alert("Please enter a postcode!");
    </script>
  </body>
</html>

For this to work, you need to make sure that ASP code blocks are correctly encapsulated in <% and %> tags.为此,您需要确保 ASP 代码块正确封装在<%%>标记中。

The other option is to use Response.Write() but it can become unwieldy.另一种选择是使用Response.Write()但它可能变得笨拙。

<% @Language="VBScript" CodePage=65001 %>
<%
Response.Charset = "UTF-8"
Response.CodePage = 65001

'ASP Code here
Call Response.Write("<!doctype html>" & vbCrLf)
Call Response.Write("<html>" & vbCrLf)
Call Response.Write("  <head>" & vbCrLf)
Call Response.Write("    <title>Test Page</title>" & vbCrLf)
Call Response.Write("  </head>" & vbCrLf)
Call Response.Write("  <body>" & vbCrLf)
'ASP Code here
Call Response.Write("    <script type=""text/javascript"">" & vbCrLf)
Call Response.Write("      alert(""Please enter a postcode!"");" & vbCrLf)
Call Response.Write("    </script>" & vbCrLf)
Call Response.Write("  </body>" & vbCrLf)
Call Response.Write("</html>")
%>

You also have to look out for issues with escaping literal double-quotes in the strings by doubling them.您还必须通过加倍查找字符串中 escaping 文字双引号的问题。

However, there is another approach that basically takes the best of both worlds to build a very structured page.然而,还有另一种方法,基本上是两全其美来构建一个非常结构化的页面。

<% @Language="VBScript" CodePage=65001 %>
<%
Option Explicit
Response.Charset = "UTF-8"
Response.CodePage = 65001

'Entry point for the page logic.
Call init()

Sub init()
   'Initiate any variables from query string / form posts here.

   Call page()
End Sub

Sub page()
%>
<!doctype html>
<html>
  <head>
    <title>Test Page</title>
  </head>
  <body>
<%
'ASP Code here
%>
    <script type="text/javascript">
      alert("Please enter a postcode!");
    </script>
  </body>
</html>
<%
End Sub
%>
<% if (your ASP code) then %>

<script type="text/javascript">
alert('Please enter a postcode!');
</script>

<% end if %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM