PDA

Click to See Complete Forum and Search --> : the presence of objects


i00
Apr 21st, 2004, 04:13 PM
i currently do this in one of my pages:

parent.document.getElementById('UseageBar').innerHTML = "bla";

but the UseageBar (or the parent too on occasions) will not always exist - how can i detect their presence first, so that i do not get an error when displaying my page?

thanks
Kris

visualAd
Apr 21st, 2004, 04:18 PM
if (parent)
{
if (parent.document.getElementById('UseageBar'))
{
...
..

Acidic
Apr 21st, 2004, 04:24 PM
is the first if (parent) necessary?
The 2nd if will still see if the thing exists, if it doesn't it wont access it. I don't think the 1st if is necessary

visualAd
Apr 21st, 2004, 04:32 PM
Probably not - but I garuntee it will work :D

CornedBee
Apr 22nd, 2004, 03:20 AM
If you don't check for the existence of parent and then try to access it as in the second if you'll get a JavaScript error. Most people won't even notice, but it can cause unexpected effects.

For example

if(parent.document.getElementById('some')) {
// This is executed if both parent and some exist.
} else {
// This is executed if parent exists but some doesn't.
}
// Neither part, nor anything here, is executed if parent doesn't exists.
// A JS error cancels script execution.

You could use try...catch to find out if parent doesn't exist.