PDA

View Full Version : How to check and avoid "has no properties" error?


calbears
12-01-2006, 12:18 PM
Hello,

How do I check if a variable has any properties so I can avoid these error in Javascript?

message has no properties
message.childNodes[0] has no properties


For example, I'll get these errors when the XML returns nothing.(<message></message>)

The alert just shows "message = undefined length undefined".

Here's my code. Thanks.



var message = http.responseXML.getElementsByTagName("message")[0];

alert ("message = " + message.value + " length = " + message.length);

if (message)
{
results = message.childNodes[0].nodeValue.split(",");
....

BonRouge
12-01-2006, 01:11 PM
Did you try 'if (message.childNodes[0])' instead of 'if (message)' ?

calbears
12-01-2006, 02:22 PM
Hi BonRouge,

No I did not. That did the trick. Thanks.

Jon Hanlon
12-03-2006, 05:13 PM
and message.childNodes.length tells you how many childNodes there are.