SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check for variable/ value existence

    Is there a way of checking for variables existence without error. I'm trying to check if a form element exists -

    if (window.opener.document.forms[0].absent_table.value)
    {
    // do something
    }

    whenever this form element isn't present, I'm informed with an ugly error message.

    This is perfectly possible in PHP with the isset(), empty() functions – which has no relevance to my problem at hand.

    Many thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Needham, MA
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is just a guess, but try testing for the element without the ".value" at the end:

    if(window.opener.document.forms[0].absent_table)
    Tom Mollerus
    tmollerus@oneworkspace.com
    http://www.oneworkspace.com The affordable, simple, and secure way to manage your projects online

  3. #3
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Removing value will prevent the error, why?? javascript is annoyingly frustrating at times!

    if (window.opener.document.forms[0].absent_table)
    {
    // do something
    }

  4. #4
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is just a guess, but try testing for the element without the ".value" at the end:

    if(window.opener.document.forms[0].absent_table)
    Thanks, for your post, I'd since discovered this.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by richard_h
    Removing value will prevent the error, why??
    Because if the element doesn't exist, you surely can't access it's value property, now can you?

    FYI, the "proper" way to do this sort of test is like this

    if ( typeof window.opener.document.forms[0].absent_table == 'undefined' )

    But in actual application, the short-circuit check will work too.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because if the element doesn't exist, you surely can't access it's value property, now can you?
    Neither the element or the value exist, so logically both conditions should evaluate to false.

    if(window.opener.document.forms[0].absent_table.value)

    if(window.opener.document.forms[0].absent_table)

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by richard_h
    Neither the element or the value exist, so logically both conditions should evaluate to false.
    True, but that is a false assumption, and logic will actually point you in the right direction.

    Pretend your form is a building, all it's elements are rooms, and the properties (such as value) are chairs. How do you count the chairs in a room that doesn't exist? (answer: you don't -- because not only is there no room, but no chairs to count)

    So basically, without the element in question existing, you can't skip straight the property and check -- just as much as you can't go counting chairs in a room that doesn't exist -- you must check for a room first! This is why you probably got an "Object Expected" error -- the script engine was looking for that room (err, element)

    Just perform a multiple condition if you want to test both at the same time.

    var f = window.opener.document.forms[0]
    if ( typeof f.absent_table == 'undefined' || f.absent_table.value == '' )
    // element doesn't exist or is blank
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could argue if there is no room you wouldn't attempt to count the chairs and you could also argue that I should just except JavaScript is less forgiving than other languages I'm used to

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, javascript is fairly loosely typed as far as I'm concerned. What are you used to?

    An yes, you could argue that if there was no room you wouldn't attempt to count the chairs, which is exactly why you shouldn't (and can't) attempt to acccess the value property when there is no element.

    P.S. You did mean accept when you said except, right?

    P.P.S. The really isn't about javascript at all, but the DOM. vbscript would throw the same error.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    SitePoint Addict richard_h's Avatar
    Join Date
    May 2002
    Location
    London
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You did mean accept when you said except, right?
    Yeah! I've got one of those forgiving grammer checkers

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, you got it all straightened out then?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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