JavaScript typeof checking a Request value

Ask a Question related to ASP, Design and Development.

  1. #1

    Default 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
    :)


    Robert Mark Bram Guest

  2. Similar Questions and Discussions

    1. What is the difference between REQUEST and REQUEST.QUERYSTRING?
      What is the difference between these two statements? They seem to do the same thing... response.write(request("variable")) ...
    2. Confused about a REQUEST.FORM and a REQUEST.QUERYSTRING
      This is snipit of code, supplied by PayPal with explanation about what has to be done to access their back end. I am confused because they first...
    3. Checking for Javascript funcionality
      Hi people, I think is the PHP scripter needs to know in advance if he can count on Client-side scripting capability. I just start with PHP but...
    4. best way to get data: request.form, request.params, controlname.value
      Hi! I think I remember somewhere that using request.form was a bad idea (I can't say I remember why). So I'm wondering: What is the best way to...
    5. difference bet. request.querystring and Request.Params
      request.params for asp.net is the httprequest object, and this method gets a combined collection of querystring, cookies, form and servervars...
  3. #2

    Default Re: JavaScript typeof checking a Request value

    "Robert Mark Bram" <relaxedrob@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 - [email]lrn@hotpop.com[/email]
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen Guest

  4. #3

    Default Re: JavaScript typeof checking a Request value

    > 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 ([url]http://www.grisoft.com[/url]).
    Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/03


    The Mighty Chaffinch Guest

  5. #4

    Default Re: JavaScript typeof checking a Request value

    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
    :)


    Robert Mark Bram Guest

  6. #5

    Default Re: JavaScript typeof checking a Request value

    > 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 ([url]http://www.grisoft.com[/url]).
    Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/03


    The Mighty Chaffinch Guest

  7. #6

    Default Re: JavaScript typeof checking a Request value

    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
    :)


    Robert Mark Bram Guest

  8. #7

    Default Re: JavaScript typeof checking a Request value

    On Sun, 19 Oct 2003 12:28:15 +1000, "Robert Mark Bram"
    <relaxedrob@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
    Steve van Dongen Guest

  9. #8

    Default Re: JavaScript typeof checking a Request value

    On Sat, 18 Oct 2003 22:04:04 +0000 (UTC), "The Mighty Chaffinch"
    <mightychaffinch@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
    Steve van Dongen Guest

  10. #9

    Default Re: JavaScript typeof checking a Request value

    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
    :)


    Robert Mark Bram Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139