简体   繁体   中英

Call javascript function in specific point in visual basic code in classic asp

So I have visual basic code in a classic asp page where I want to get a javascript error message to display if my code reaches a certain point:

<%
Function parseString(inputString){
 'some code
 if startPos > 0 Then
  'some code
 Else
  displaymessage
 End if
}
'more code
%>

<script type ="text/javascript">
    function displaymessage() {
        alert("Wrong!");
    }
</script>

How do I get the displaymessage function to execute at a specific point in my visual basic code?

asp executes on the server. Javascript executes on the client. You could write the code to execute the function into a script block that would then execute client:

<%
Function parseString(inputString){
 'some code
 if startPos > 0 Then
  'some code
 Else
  'out your javascript code here
  Response.Write "displayMessage();"  
 End if
}
%>
<script language="javascript">
  <%parseString("some string to parse")%>
</script>

You could also do this:

<%
Function parseString(inputString)
   if startPos > 0 Then
       '...
   Else
       call displayMessage
   End if
End Function

Function displayMessage
%>
    <script type ="text/javascript">
        alert("Wrong!");
    </script>
<%
End Function
%>

But you have to realise that (as Lee said) vb code runs on the server and only after it is done, its result will be sent to the client and there your js code will run. This means that your vb code will not be stopped by this alert. Moreover, this alert may not even display on the client because just blindly writing this piece of javascript code to the response may not result in a valid document.

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