JavaScript typeof checking a Request value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    #1

    JavaScript typeof checking a Request value

    Hi All!

    I have the following code in an asp page whose language tag is:
    <%@LANGUAGE="JA VASCRIPT" 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.as p");
    } // 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
    :)


  • Lasse Reichstein Nielsen

    #2
    Re: JavaScript typeof checking a Request value

    "Robert Mark Bram" <relaxedrob@rem ovethis.optusho me.com.au> writes:
    [color=blue]
    > Response.Write( "Edition is type &quot;" + (typeof edition) + "&quot; and
    > value &quot;" + edition + "&quot;<br> ");[/color]
    ....[color=blue]
    > if (edition == "undefined" ||
    > (typeof edition) == "undefined" )[/color]
    ....[color=blue]
    > Edition is type "object" and value "undefined"[/color]

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

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

    Likewise, (typeof edition)=="unde fined" fails since (typeof
    edition)=="obje ct".
    [color=blue]
    > 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" ...[/color]

    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 - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Robert Mark Bram

      #3
      Re: JavaScript typeof checking a Request value

      Hi Lasse - thank you for your response!

      [color=blue][color=green]
      > > Response.Write( "Edition is type &quot;" + (typeof edition) + "&quot;[/color][/color]
      and[color=blue][color=green]
      > > value &quot;" + edition + "&quot;<br> ");[/color]
      > ...[color=green]
      > > if (edition == "undefined" ||
      > > (typeof edition) == "undefined" )[/color]
      > ...[color=green]
      > > Edition is type "object" and value "undefined"[/color]
      >
      > So, edition is an *object*, and when it is converted to a string, it
      > becomes the string "undefined" . I.e., edition.toStrin g() == "undefined" .[/color]

      I just tried this and got something even more confusing:

      if (edition.toStri ng() == "undefined" )
      {
      Response.Write( "Choose Page<br>");
      Server.Execute ("choosePage.as p");
      } // 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.toStri ng() == "undefined" )

      How can it be an Object and not have toString?

      Rob
      :)


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: JavaScript typeof checking a Request value

        "Robert Mark Bram" <relaxedrob@rem ovethis.optusho me.com.au> writes:
        [color=blue]
        > I just tried this and got something even more confusing:[/color]

        Indeed!
        [color=blue]
        > if (edition.toStri ng() == "undefined" )[/color]
        ....[color=blue]
        > 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.toStri ng() == "undefined" )
        >
        > How can it be an Object and not have toString?[/color]

        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.prototyp e, 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:undefin ed<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 - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • The Mighty Chaffinch

          #5
          Re: JavaScript typeof checking a Request value

          > I have the following code in an asp page whose language tag is:[color=blue]
          > <%@LANGUAGE="JA VASCRIPT" CODEPAGE="65001 "%>
          >
          > // Find request variables.
          > var edition = Request.Form ("edition");
          > var language = Request.Form ("language") ;[/color]

          I think you probably want the String value stored in the Request.Form.It em
          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!
          [color=blue]
          > Response.Write( "Edition is type &quot;" + (typeof edition) + "&quot;[/color]
          and[color=blue]
          >...(snipped)[/color]


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


          Comment

          • Robert Mark Bram

            #6
            Re: JavaScript typeof checking a Request value

            Hi MightyC! :)

            Thanks for the response!
            [color=blue]
            > var edition = String (Request.Form ("edition")) ,
            > language = String (Request.Form ("language") );[/color]

            OK, well this gets out the values as Strings..
            [color=blue]
            > 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.[/color]

            I spent a lot of time learning Javascript - I want to keep using it!
            [color=blue]
            > PS. I hope your user never types in "undefined" as a value![/color]

            How on earth am I going to get around this?

            Why isn't this a problem with VBScript?

            Rob
            :)


            Comment

            • Robert Mark Bram

              #7
              Re: JavaScript typeof checking a Request value

              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:undefin ed<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:undefine d
              in for
              end for

              Interestingly, I can do this with the same results:

              var edition = Request.Form ("edition").val ue;
              var language = Request.Form ("language").va lue;

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


              Comment

              • The Mighty Chaffinch

                #8
                Re: JavaScript typeof checking a Request value

                > Thanks for the response!

                You're welcome!
                [color=blue][color=green]
                > > var edition = String (Request.Form ("edition")) ,
                > > language = String (Request.Form ("language") );[/color]
                >
                > OK, well this gets out the values as Strings..[/color]

                It will invoke the toString method for the object, which is what you want.
                [color=blue][color=green]
                > > 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.[/color]
                >
                > I spent a lot of time learning Javascript - I want to keep using it![/color]

                Amen to that!
                [color=blue][color=green]
                > > PS. I hope your user never types in "undefined" as a value![/color]
                >
                > How on earth am I going to get around this?[/color]

                I use code like this:

                var action = (typeof (Request.Form.I tem ("action")) == "undefined" )
                ? null
                : String (Request.Form.I tem ("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.
                [color=blue]
                > Why isn't this a problem with VBScript?[/color]

                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


                Comment

                • Robert Mark Bram

                  #9
                  Re: JavaScript typeof checking a Request value

                  Hi again!
                  [color=blue]
                  > I use code like this:
                  >
                  > var action = (typeof (Request.Form.I tem ("action")) == "undefined" )
                  > ? null
                  > : String (Request.Form.I tem ("action"));[/color]

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

                  and not "undefined"

                  Rob
                  :)


                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: JavaScript typeof checking a Request value

                    Lasse Reichstein Nielsen wrote:[color=blue]
                    > There is one value with a typeof of "object" that isn't an object: the
                    > null value. [...][/color]

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

                    See

                    and ECMAScript Edition 3, sections 4.3.11 and 4.3.12.
                    (<http://www.ecma-international.o rg/publications/standards/Ecma-262.htm>)


                    PointedEars

                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: JavaScript typeof checking a Request value

                      Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                      [color=blue]
                      > Lasse Reichstein Nielsen wrote:[color=green]
                      > > There is one value with a typeof of "object" that isn't an object: the
                      > > null value. [...][/color]
                      >
                      > `null' is a special object literal, the primitive sole value of the
                      > Null type. It "represents the null, empty, or non-existent reference."[/color]

                      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 - lrn@hotpop.com
                      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Steve van Dongen

                        #12
                        Re: JavaScript typeof checking a Request value

                        On Sun, 19 Oct 2003 12:28:15 +1000, "Robert Mark Bram"
                        <relaxedrob@rem ovethis.optusho me.com.au> wrote:
                        [color=blue]
                        >Hi again!
                        >[color=green]
                        >> I use code like this:
                        >>
                        >> var action = (typeof (Request.Form.I tem ("action")) == "undefined" )
                        >> ? null
                        >> : String (Request.Form.I tem ("action"));[/color]
                        >
                        >Except that I am finding this:
                        > typeof (Request ("notThere") )
                        >or
                        > typeof (Request.Form.I tem ("notThere") )
                        >returns
                        > "object"
                        >
                        >and not "undefined"[/color]

                        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("f oo").Count == 0
                        ? null
                        : Request.Form("f oo").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=45 6 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

                        Comment

                        • Steve van Dongen

                          #13
                          Re: JavaScript typeof checking a Request value

                          On Sat, 18 Oct 2003 22:04:04 +0000 (UTC), "The Mighty Chaffinch"
                          <mightychaffinc h@hotmail.com> wrote:
                          [color=blue][color=green][color=darkred]
                          >> > var edition = String (Request.Form ("edition")) ,
                          >> > language = String (Request.Form ("language") );[/color]
                          >>
                          >> OK, well this gets out the values as Strings..[/color]
                          >
                          >It will invoke the toString method for the object, which is what you want.[/color]

                          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

                          Comment

                          • Robert Mark Bram

                            #14
                            Re: JavaScript typeof checking a Request value

                            Howdy Steve - thank you very much for your response!

                            (from your other reply)[color=blue]
                            > 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().[/color]

                            *the fog begins to lift* .. :)
                            [color=blue]
                            > Yes, that's correct, it's a collection.[/color]
                            ....[color=blue]
                            > var foo = Request.Form("f oo").Count == 0
                            > ? null
                            > : Request.Form("f oo").item()[/color]

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

                            Rob
                            :)


                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: JavaScript typeof checking a Request value

                              Lasse Reichstein Nielsen wrote:
                              [color=blue]
                              > 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).[/color]

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


                              PointedEars

                              Comment

                              Working...