Exception Handling for Undefined element while creating its object

M

Moses

Hi All,

Is is possible to catch the error of an undefined element while
creating an object for it.

Consider we are not having an element with id indicator but we
are trying to make the object for it

indicator = document.getElementById('indicator');

Though we dont have any element like 'indicator' we wont get any
error

but if we try like this

indicator.style.innerHTML = 'Loading'

which throws the error "Indicator has no Properties"


Is it possible to catch the error while creating an object
itself


(ie)

try
{
indicator = document.getElementById('indicator');

}
catch(e)
{
//No Error

}


but

try
{
indicator.style.innerHTML = 'Loading'
}
catch(e)
{
// Here we get the error

}



But I need to cache the error while Creating the object itself , ie
If I try to create an object for an undefined element the error
should be thrown.

Is it posible.

Thanks & Regards
Moses
 
R

RobG

Hi All,

Is is possible to catch the error of an undefined element while
creating an object for it.

Consider we are not having an element with id indicator but we
are trying to make the object for it

indicator = document.getElementById('indicator');

Though we dont have any element like 'indicator' we wont get any
error

If no element with id="indicator" exists, then the variable indicator
should be set to null.

but if we try like this

indicator.style.innerHTML = 'Loading'

which throws the error "Indicator has no Properties"

Because you are trying to do something with a property of an object
that doesn't exist. Even if indicator is a reference to DOM element,
its style object doesn't have an innerHTML property, that is a
property of the element itself - maybe.
Is it possible to catch the error while creating an object
itself

(ie)

You me e.g.
try {
indicator = document.getElementById('indicator');
} catch(e) {
//No Error

It will enter the catch block if there *is* an error.
}

but

try {
indicator.style.innerHTML = 'Loading'
} catch(e) {
// Here we get the error
}

But I need to cache the error while Creating the object itself , ie
If I try to create an object for an undefined element the error
should be thrown.

You're confused. If I understand your question (and it's very likely
I don't), the error occurs because you are incorrectly using
innerHTML.

Is it posible.

Yes, test the return value of getElementById to ensure it returns an
object. Then test that the properties of that object are what you
expect before you try to access properties of the properties...

var indicator = document.getElementById('indicator');
if (indicator && typeof indicator.innerHTML == 'string') {
// do something with indicator's innerHTML property
}
 
M

Moses

Hi,

Though I have confused U have replied me what I want,

var indicator = document.getElementById('indicator');
if (!indicator)
{
throw 'Please Create the indicator Element';
}

Thank you.

Moses
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top