473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JavaScript typeof checking a Request value

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
:)
Jul 20 '05 #1
15 12737
"Robert Mark Bram" <re********@removethis.optushome.com.au> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Hi Lasse - thank you for your response!

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".


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
:)
Jul 20 '05 #3
"Robert Mark Bram" <re********@removethis.optushome.com.au> writes:
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[i]+"<br>);
}
}

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

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
> 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!
Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and...(snipped)

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/03
Jul 20 '05 #5
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
:)
Jul 20 '05 #6
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[i]+"<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
:)
Jul 20 '05 #7
> Thanks for the response!

You're welcome!
var edition = String (Request.Form ("edition")),
language = String (Request.Form ("language"));
OK, well this gets out the values as Strings..


It will invoke the toString method for the object, which is what you want.
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!


Amen to that!
PS. I hope your user never types in "undefined" as a value!


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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/03
Jul 20 '05 #8
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
:)
Jul 20 '05 #9
Lasse Reichstein Nielsen wrote:
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."

See
http://devedge.netscape.com/library/...t.html#1008306
and ECMAScript Edition 3, sections 4.3.11 and 4.3.12.
(<http://www.ecma-international.org/publications/standards/Ecma-262.htm>)
PointedEars

Jul 20 '05 #10
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Lasse Reichstein Nielsen wrote:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11
On Sun, 19 Oct 2003 12:28:15 +1000, "Robert Mark Bram"
<re********@removethis.optushome.com.au> wrote:
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"


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
Jul 20 '05 #12
On Sat, 18 Oct 2003 22:04:04 +0000 (UTC), "The Mighty Chaffinch"
<mi*************@hotmail.com> wrote:
> var edition = String (Request.Form ("edition")),
> language = String (Request.Form ("language"));


OK, well this gets out the values as Strings..


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
Jul 20 '05 #13
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
:)
Jul 20 '05 #14
Lasse Reichstein Nielsen wrote:
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

Jul 20 '05 #15
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
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[i]={};
}
alert(a[0]==a[1]);
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.