-
the presence of objects
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
-
Code:
if (parent)
{
if (parent.document.getElementById('UseageBar'))
{
...
..
-
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
-
Probably not - but I garuntee it will work :D
-
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
Code:
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.