Matt W
Matt W

Reputation: 4133

ASP If, Else If only evaluates first statement

I'm relatively competent with javascript. I thought asp might be as forgiving, but I'm pulling my hair out over this.

I want to determine if the request url has a specific query string in order to run some code. However, (my) ASP seems to fail if the exact string isn't matched:

<% If Request.QueryString("somestring") Then%> target="_blank" <% Else If Not Request.QueryString() Is Nothing Then%> <% End If %>

Using a catch-all also fails without a proper match:

<% If Request.QueryString("somestring") Then%> target="_blank" <% Else %> <% End If %>

So www.example.com?somestring=true works fine, however when I remove or change any part of the query string an error is thrown.

Does this method require a query string to be present? Is there a way to make this work if there is no query string and a string that doesn't match?

Any help with this would be amazing. Thank you :)

Upvotes: 2

Views: 195

Answers (1)

ekad
ekad

Reputation: 14614

Assuming that you want to display target="_blank" if somestring query string has a value, then this would work:

<% If Request.QueryString("somestring") IsNot Nothing Then%> target="_blank" <% Else %> <% End If %>

If you want to compare the query string to a specific value, let's say JS:

<% If Request.QueryString("somestring") = "JS" Then%> target="_blank" <% Else %> <% End If %>

Upvotes: 2

Related Questions