3

Possible Duplicate:
Finding out if a URL param exists in JS-ASP

this may be very fundamental but,

in the code below, the if branch is to cover the abnormal situation which the hitting request miss the query parameter of "name", like http://foo.com/bar.asp?foo=bar

<%@ language="javascript" %>

<html>
<head>
    <title>Hello ASP!</title>
</head>
<body>
    <%
        if (Request.QueryString("name") == undefined)
        {
            Response.Write("oops, give me your name!");
        }
        else
        {
            Response.Write("Hello " + Request.QueryString("name") + "!");
        }
    %>
</body>
</html>

but the if branch was never entered. The else branch takes place every time, and I got "Hello undefined!" every time.

I`ve tried to replace the undefined with "" or null, but nothing changes.

I`ve searched around but got things talking about ASP.NET/C# and client-side JavaScript. Appreciate your help!

UPDATED:
HI guys, thanks for your concern! But I mean ASP leveraging JavaScript to do server-side programming. I`ve updated the codes above.

Community
  • 1
  • 1
Lyn
  • 699
  • 1
  • 7
  • 17
  • 1
    This is not classic asp. In classic asp that would be `if request.querystring("name") = "" then ... else ... end if` – Andreas May 20 '12 at 13:11
  • hi Adreas, thanks for your concern. I guess you are using VBScript right? I`m new to ASP and don`t know VBScript, but I`ve learned a little bit JS, so I wish to ues JS. – Lyn May 20 '12 at 13:24
  • there is solution like "if ("" + Request.QueryString("name") == "undefined")", but I think it`s tricky. – Lyn May 20 '12 at 13:41

2 Answers2

3

The most reliable way to check whether a parameter was passed or not is to check the Count property:

if (Request.QueryString("name").Count === 0) {
    // do whatever...
}

This works because Request.QueryString(var) actually returns an array-like object that contains multiple values, if the same parameter is specified multiple times in the URL (e.g., http://example.com/test.asp?name=value1&name=value2). You can read more in the MSDN documentation:

http://msdn.microsoft.com/en-us/library/ms524784%28VS.90%29.aspx

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
0

Perhaps this would work for you:

<script type="text/javascript">
// access the server-side response 
// and store it in a javascript variable
var myQueryString = '<%= Request.QueryString("name") %>';

// now write to the document using javascript only
if (myQueryString === "" ) {
   document.write("oops, give me your name!");
} else {
   document.write("Hello " + myQueryString + "!");
}
</script>
mg1075
  • 17,985
  • 8
  • 59
  • 100
  • 1
    I haven't done ASP classic in a very long time, but I do remember that you had your choice of VBScript of JScript as your server-side language. Not many people went the JScript route though. – Matthew Nichols May 20 '12 at 14:10
  • Hi @mg1075, my codes are completely JavaScript codes at server side. I learned that ASP can leverage JavaScript to do server-side programming right? The JS code above are to be run at client-side, except that in the <% .. %>? – Lyn May 20 '12 at 14:30