0

Maybe it's trivial question, but I have no idea what is the type of Request.QueryString(). When I check it with typeof - it says it's Object. How to check which object it is and which property of that object is a string in URL?

I'm doing it server side with language specification <%@ language="javascript"%>

If I've got URL like that: http://127.0.0.1/Kamil/Default.asp?name= then how to check if name is empty? It's not null. I know I can convert Request.QueryString("name") to String and check if it's empty string "", but is it proper way?

alcohol is evil
  • 686
  • 12
  • 34
  • 2
    Don't confuse JavaScript and JScript, they are different. – user692942 Jul 15 '15 at 00:01
  • 1
    @Dijkgraaf This isn't JavaScript it is Microsoft's flavour of JavaScript used in its server side scripting language Active Server Pages *(known now as Classic ASP)* don't confuse the two this server side version *(denoted by the `<% @language = "JavaScript" %>` is actually called [tag:JScript] confusing I know but don't confuse the two. – user692942 Jul 15 '15 at 07:25
  • Worth reading the [jscript tag wiki](http://stackoverflow.com/tags/jscript/info) to get a better explanation. – user692942 Jul 15 '15 at 07:36
  • Also this from the horses mouth - http://stackoverflow.com/a/28331933/692942 *([@Dai](http://stackoverflow.com/users/159145/dai) is an engineer on Microsoft's JavaScript team)* – user692942 Jul 15 '15 at 09:39
  • Just to directly answer the title, (practically) all variables in Classic ASP are of [type `variant`](http://www.functionx.com/asp/Lesson05.htm), though (somewhat confusingly) there are variant subtypes. – Paul Jul 15 '15 at 10:18
  • Worth mentioning -- you can check this yourself in VBScript with the `typename()` function -- i.e. `typename( Request.QueryString )` – Stephen R Nov 17 '17 at 18:26

2 Answers2

4

The Request collection is an object that inherits IRequestDictionary interface.
In JScript, it's a good practice to use item to get actual value, not the implicit one due to values of the QueryString collection (also Form and ServerVariables) are IStringList in fact.
You said you know C# so you'll understand the following fictitious QueryString declaration.

var QueryString = new IRequestDictionary<string, IStringList>();

And a few examples how you should check the values in JScript without string conversion.

if(Request.QueryString("name").count==0){
    // parameter does not exist
}

if(Request.QueryString("name").item==undefined){
    // parameter does not exist
}

if(Request.QueryString("name").item!=undefined){
    // parameter exists and may be empty
}

if(Request.QueryString("name").item){
    // parameter exists and non-empty
}
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
3

The result of Request.Querystring (with no name provided) is a string. The result of Request.Querystring("name") depends on whether "name" is part of the querystring, and if it is, whether it has a value.

So, given the following querystring:

 mypage.asp?A=1&B=

If you read them into variables:

x = Request.Querystring("A")
y = Request.Querystring("B")
z = Request.Querystring("C")

you'll get the following results: x = "1" (a string), y = "" (a blank string), and z = Empty (a special value which will be silently converted to a string or a number depending on how you use it, i.e. both Empty = "" and Empty = 0 are in some sense true).

Martha
  • 3,932
  • 3
  • 33
  • 42
  • You're right. `typeof Request.Querystring("name")` shows object, but `typeof new String()` is also object, so it's ok. Javascript doesn't recognize Empty keyword. Variable `z` from your example is not null, not undefined and not empty string. But when converted to String - it's "undefined" (but before conversion it's also object). Can you explain that? – alcohol is evil Jul 14 '15 at 19:57
  • @alcoholisevil: I'm sorry, I guess I should have been more clear: what I posted is for VBScript, since your asp-classic tag made me assume that that's what you'd be using. Javascript and I aren't on speaking terms, so if you're actually trying to do this client-side (why??), you'll have to ask someone else. – Martha Jul 14 '15 at 20:30
  • 1
    Ok, thank you. I'm doing it on server side with language specification `<%@ language="javascript"%>`. I'm trying to understand ASP before I start learning ASP.NET MVC. I've learned javascript, but I don't want to learn VBScript. – alcohol is evil Jul 14 '15 at 20:41
  • 4
    @alcoholisevil: ah, I see. In that case, I'm afraid my answer is not going to be useful to you. If you want my advice, though, classic ASP and ASP.NET are totally different; knowing the former will not really help you with the latter. If what you want to use is asp.net, just learn asp.net. – Martha Jul 14 '15 at 20:51
  • 2
    And furthering on the above comment, you will not be able to use javascript as a server side language in ASP.net, so unless you have a pressing need to learn classic ASP go straight to ASP.net (my preference there would be in C#). Also the vast majority of Classic ASP is written in VBScript, so if you're going bother with ASP classic, use VBScript. – Jon P Jul 15 '15 at 01:24
  • 1
    @JonP Classic ASP doesn't have to use VBScript that is just the default out of the box, it can use JScript along with other Active Scripting Languages. There is no requirement to use VBScript at all they are completely interchangeable. – user692942 Jul 15 '15 at 07:32
  • VBScript is so strange, I don't understand it at all... I know classic ASP is different than ASP.NET but I want to know it a bit before I start learning ASP.NET because I think it's cool and it's good to know what it is. I'm not going to concentrate on it for long time. @Jon P, I know C# and I will use it in ASP.NET. – alcohol is evil Jul 15 '15 at 14:30
  • @Lankymart, I never said you can't use javascript, just *most* classic ASP is in VBScript – Jon P Jul 15 '15 at 22:08
  • @JonP That isn't what you said but if it is what you meant that's fine. – user692942 Jul 15 '15 at 22:15
  • BTW Why in `<%@ language="javascript" %>` it is "javascript"? Why it's not "jscript"? Did Microsoft want to motivate programmers which don't know VBScript to use ASP or the reason is different? – alcohol is evil Jul 19 '15 at 15:19
  • 1
    @alcoholisevil It's both you can specify `<%@ language="jscript" %>` and it will work the same `javascript` is just an alias. – user692942 Jul 24 '15 at 14:38