Search code examples
javascriptvbscriptasp-classic

Classic ASP query string variable is both set and unset


I have this code:

<%If CInt(Request.QueryString("OpenYouthHistory")) > 0 Then %>
<script>
    var yid = <% Request.QueryString("OpenYouthHistory") %>;
    window.open("YouthHistory.asp?YouthID=" + yid);
</script>
<% End If %>

I want to open the YouthHistory.asp page in a popup window if the OpenYouthHistory query string variable is set. However when I run it with a value of, say, 210, then I get this output:

<script>
    var yid = ;
    window.open("YouthHistory.asp?YouthID=" + yid);
</script>

Which of course is invalid JavaScript. But why is the yid variable not getting a value? If the query string variable really is blank, why is the <script> tag even rendering? I'm so confused...


Solution

  • Use <%= which is a shortcut for <% Response.Write

    <%If CInt(Request.QueryString("OpenYouthHistory")) > 0 Then %>
    <script>
        var yid = <%=Request.QueryString("OpenYouthHistory") %>;
        window.open("YouthHistory.asp?YouthID=" + yid);
    </script>
    <% End If %>