简体   繁体   中英

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:

  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:

response.write("<SCRIPT language=" & chr(34) & "javascript" & chr(34) & ">alert('Please enter a postcode;');</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
  2. The <SCRIPT> tag.

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).

I've been looking at a couple of other questions on StackOverlow and Inte.net searches:

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.

Thanks @user692942 - that was definitely helpful: what I did in the end was to add the following to the ASP page:

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 .

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

The other option is to use Response.Write() but it can become unwieldy.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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