Check if object exist

Discussion in 'Javascript' started by Mateo, Aug 23, 2007.

  1. Mateo

    Mateo Guest

    Hi!

    Let's say that I have generated var SomeVariable = true; from server.
    On client side I have:
    if (SomeVariable == true) {
    do something
    }

    How could I first check if my variable is undefined? Or how can I say if
    some exception occures please don't report error, just continiue?
    If variable is not generated from server, it will report me an error....


    Thanx guys!
     
    Mateo, Aug 23, 2007
    #1
    1. Advertising

  2. Mateo

    RobG Guest

    On Aug 23, 8:16 pm, "Mateo" <> wrote:
    > Hi!
    >
    > Let's say that I have generated var SomeVariable = true; from server.
    > On client side I have:
    > if (SomeVariable == true) {
    > do something
    >
    > }
    >
    > How could I first check if my variable is undefined?


    if (typeof someVariable != 'undefined')
    {
    // someVariable is not undefined, but
    // might be null, or false, or whatever...
    }

    But if you are expecting it to be boolean true, why not:

    if (someVariable) ...


    --
    Rob
     
    RobG, Aug 23, 2007
    #2
    1. Advertising

  3. Mateo

    Mateo Guest


    >> How could I first check if my variable is undefined?

    >
    > if (typeof someVariable != 'undefined')
    > {
    > // someVariable is not undefined, but
    > // might be null, or false, or whatever...
    > }



    thank u, this works fine....

    > But if you are expecting it to be boolean true, why not:


    this produces error to...

    > if (someVariable) ...
    >
    >
    > --
    > Rob
    >
     
    Mateo, Aug 23, 2007
    #3
  4. Mateo

    Mateo Guest

    thank you, your solution works fine too... this looks like old browsers
    compatibile....

    > if(window['someVariable'])
    > {
    > alert('It exists')
    > }
    > else
    > {
    > alert('It doesnt exist')
    > }
     
    Mateo, Aug 23, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    3
    Views:
    3,129
  2. Y.S.
    Replies:
    3
    Views:
    1,042
    strajan
    Sep 17, 2003
  3. LT
    Replies:
    7
    Views:
    2,110
    Phlip
    Jul 25, 2004
  4. QQ
    Replies:
    1
    Views:
    1,001
    Andrew Thompson
    Oct 5, 2007
  5. Markus Mohr
    Replies:
    7
    Views:
    261
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page