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, e.g.:
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:
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 Internet 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.
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
%>