JavaScript typeof checking a Request value

R

Robert Mark Bram

Hi All!

I have the following code in an asp page whose language tag is:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>

// Find request variables.
var edition = Request.Form ("edition");
var language = Request.Form ("language");
Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and
value &quot;" + edition + "&quot;<br>");
Response.Write("Language is type &quot;" + (typeof language) + "&quot;
and value &quot;" + language + "&quot;<br>");

if (edition == "undefined" ||
(typeof edition) == "undefined")
{
Response.Write("Choose Page<br>");
Server.Execute ("choosePage.asp");
} // end if
else
{
Response.Write("View Page<br>");
Server.Execute ("viewPage.asp");
} // end else

The problem is this is what I see:

Edition is type "object" and value "undefined"
Language is type "object" and value "undefined"
View Page


There is nothing in choosePage.asp or viewPage.asp yet, but I am unable to
figure out why the else is triggering and not the if - my print out of the
edition var shows it has a value of "undefined"...

Any help would be most appreciated!

Rob
:)
 
L

Lasse Reichstein Nielsen

Robert Mark Bram said:
Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and
value &quot;" + edition + "&quot;<br>"); ....
if (edition == "undefined" ||
(typeof edition) == "undefined") ....
Edition is type "object" and value "undefined"

So, edition is an *object*, and when it is converted to a string, it
becomes the string "undefined". I.e., edition.toString() == "undefined".

You then test whether edition=="undefined" . It isn't, since it is an
object, not a string, and objects are only equal to themselves.

Likewise, (typeof edition)=="undefined" fails since (typeof
edition)=="object".
but I am unable to figure out why the else is triggering and not the
if - my print out of the edition var shows it has a value of
"undefined"...

No, its value is an object, which is neither the value "undefined" or
the string "undefined". That object has a method called toString that
returns the string "undefined".

/L
 
R

Robert Mark Bram

Hi Lasse - thank you for your response!

So, edition is an *object*, and when it is converted to a string, it
becomes the string "undefined". I.e., edition.toString() == "undefined".

I just tried this and got something even more confusing:

if (edition.toString() == "undefined")
{
Response.Write("Choose Page<br>");
Server.Execute ("choosePage.asp");
} // end if
else
{
Response.Write("View Page<br>");
Server.Execute ("viewPage.asp");
} // end else

The resulting error:
Error Type:
Microsoft JScript runtime (0x800A01B6)
Object doesn't support this property or method
/polyprint/newsletter.asp, line 16

Line 16 is the line:
if (edition.toString() == "undefined")

How can it be an Object and not have toString?

Rob
:)
 
L

Lasse Reichstein Nielsen

Robert Mark Bram said:
I just tried this and got something even more confusing:
Indeed!

if (edition.toString() == "undefined") ....
The resulting error:
Error Type:
Microsoft JScript runtime (0x800A01B6)
Object doesn't support this property or method
/polyprint/newsletter.asp, line 16

Line 16 is the line:
if (edition.toString() == "undefined")

How can it be an Object and not have toString?

There is one value with a typeof of "object" that isn't an object: the
null value. However, converting that to a string gives "null", not
"undefined" (at least it should according to the ECMAScript standard).

All Javascript objects have a prototype chain that ends at
Object.prototype, which has a toString method. However, native
objects can act almost arbitrarily strange.

Try this:

if (edition === undefined) {
Response.Write("undefined<br>");
}
if (edition === null) {
Response.Write("null<br>");
}
if (""+edition == "undefined") {
Repsonse.Write("string:undefined<br>");
}
if (edition !== undefined && edition !== null) {
for(var i in edition) {
Response.Write(i+"="+edition+"<br>);
}
}

If it is an object of any kind, this might shed some light on which
kind of object it is.

/L
 
T

The Mighty Chaffinch

I have the following code in an asp page whose language tag is:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>

// Find request variables.
var edition = Request.Form ("edition");
var language = Request.Form ("language");

I think you probably want the String value stored in the Request.Form.Item
object, not the object itself.
Try
var edition = String (Request.Form ("edition")),
language = String (Request.Form ("language"));

The variables should now contain either the string value from the form
field, or the string "undefined".

I hope this helps. I don't often see server-side JScript questions here,
most people seem to think that if it's ASP then it must be VBS.

MightyC

PS. I hope your user never types in "undefined" as a value!
 
R

Robert Mark Bram

Hi MightyC! :)

Thanks for the response!
var edition = String (Request.Form ("edition")),
language = String (Request.Form ("language"));

OK, well this gets out the values as Strings..
I hope this helps. I don't often see server-side JScript questions here,
most people seem to think that if it's ASP then it must be VBS.

I spent a lot of time learning Javascript - I want to keep using it!
PS. I hope your user never types in "undefined" as a value!

How on earth am I going to get around this?

Why isn't this a problem with VBScript?

Rob
:)
 
R

Robert Mark Bram

Ha!

Thank you for your reply Lasse - here is what I tried:

var edition = Request.Form ("edition");
var language = Request.Form ("language");

if (edition === undefined) {
Response.Write("undefined<br>");
}
if (edition === null) {
Response.Write("null<br>");
}
if (""+edition == "undefined") {
Response.Write("string:undefined<br>");
}
if (edition !== undefined && edition !== null) {
Response.Write("in for<br>");
for(var i in edition) {
Response.Write(i+"="+edition+"<br>");
}
Response.Write("end for<br>");
}

And the output:

string:undefined
in for
end for

Interestingly, I can do this with the same results:

var edition = Request.Form ("edition").value;
var language = Request.Form ("language").value;

I guess the only problem is there is no way to tell if it is "undefined" cos
the user entered "undefined"..

Btw, I am running IIS 5.1 on Windows XP..

Rob
:)
 
T

The Mighty Chaffinch

Thanks for the response!

You're welcome!
OK, well this gets out the values as Strings..

It will invoke the toString method for the object, which is what you want.
I spent a lot of time learning Javascript - I want to keep using it!

Amen to that!
How on earth am I going to get around this?

I use code like this:

var action = (typeof (Request.Form.Item ("action")) == "undefined")
? null
: String (Request.Form.Item ("action"));

It's a bit clumsy but I've never come up with anything better. Now you can
test for empty field (ie. == null) and use a default value or send an error
or whatever.
Why isn't this a problem with VBScript?

Dunno. Never used VBScript :)

Happy JavaScript
MightyC
 
R

Robert Mark Bram

Hi again!
I use code like this:

var action = (typeof (Request.Form.Item ("action")) == "undefined")
? null
: String (Request.Form.Item ("action"));

Except that I am finding this:
typeof (Request ("notThere"))
or
typeof (Request.Form.Item ("notThere"))
returns
"object"

and not "undefined"

Rob
:)
 
T

Thomas 'PointedEars' Lahn

L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse said:
There is one value with a typeof of "object" that isn't an object: the
null value. [...]

`null' is a special object literal, the primitive sole value of the
Null type. It "represents the null, empty, or non-existent reference."

The value "null" is actually a funny thing. It is either an object or not
an object, depending on the level you look at.

At the programming level, it is (defined to be) an object, since the
"typeof" operator returns the string "object". Apart from that, it has
nothing in common with actual objects (it has no properties and no
identity). It intended meaning to the programmer is "non-existent
reference", i.e., an initialized object reference to no object, as
opposed to "undefined", which is the uninitialized value.

At the language specification level, it is not an object. It is the
sole value of the type "null", just as the value "undefined" is the
sole value of the type "undefined".

Overall, I would not call it an object, while noticing that "typeof"
yields the string "object" anyway.


/L
 
S

Steve van Dongen

Hi again!


Except that I am finding this:
typeof (Request ("notThere"))
or
typeof (Request.Form.Item ("notThere"))
returns
"object"

and not "undefined"

Yes, that's correct, it's a collection. As I said in your other
thread I usually just convert it to a string immediately and handle
undefined later. However, this is the "proper" way to do it:

var foo = Request.Form("foo").Count == 0
? null
: Request.Form("foo").item()

item() will return a string of all the foo arguments concatenated
together and delimited by commas, for example, if you have
?foo=123&foo=456 then .item() will return "123, 456". If you could
potentially have multiple instances of the same argument and the value
may contain a comma then you should loop through the collection using
..item(i), which returns the i-th foo argument, to be sure you handle
the values correctly.

Regards,
Steve
 
S

Steve van Dongen

It will invoke the toString method for the object, which is what you want.

These are not JScript objects; they don't have a toString() method.
String(edition) calls the default method for the object which, in this
case, happens to be item().

Regards,
Steve
 
R

Robert Mark Bram

Howdy Steve - thank you very much for your response!

(from your other reply)
These are not JScript objects; they don't have a toString() method.
String(edition) calls the default method for the object which, in this
case, happens to be item().

*the fog begins to lift* .. :)
Yes, that's correct, it's a collection. ....
var foo = Request.Form("foo").Count == 0
? null
: Request.Form("foo").item()

Now that I see it is a collection, this is exactly what I wanted!

Rob
:)
 
T

Thomas 'PointedEars' Lahn

Lasse said:
The value "null" is actually a funny thing. It is either an object or not
an object, depending on the level you look at.

At the programming level, it is (defined to be) an object, since the
"typeof" operator returns the string "object". Apart from that, it has
nothing in common with actual objects (it has no properties and no
identity).

Please note that {} is an object which has no properties and no identity.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Please note that {} is an object which has no properties and no identity.

The syntax "{}" is a literal notation for an object. Its prototype
chain points to Object.prototype, so it has properties (toString,
constructor, etc.), just not any enumerable properties.

It has identity. That is why
var x={},y={}; alert(x==y);
gives false, while
var x={};alert(x==x);
gives true.

It is bordering to incorrect to say that "{}" is *an* object. It
*creates* an object, and is in fact equivalent to "new Object()". If
evaluated more than once, it crates more than one object.

var a=[];
for(var i=0;i<2;i++){
a={};
}
alert(a[0]==a[1]);


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top