Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2005, 05:54 PM
Authorized User
 
Join Date: Dec 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default AJAX and Empty Fields

I'm writing an AJAX application that retrieves a dynamically created XML file from a webserver (the xml is created using PHP). I've got the application working great except when the field is empty. In particular, the following XML

<?xml version="1">
<data>
  <date>1/2/2005</date>
  <time></time>
  <event>Something is happening</event>
</data>

As you can see, the time field is empty and when I run the javascript to parse the returned data, I get a javascript error.

I'm using the following javascript to parse the file
function parseXML() {
  var theXML = xmlHttpReq.responseXML;
  var date= theXML.getElementsByTagName('date')[0].firstChild.data;
  var time = theXML.getElementsByTagName('time')[0].firstChild.data;
  var event = theXML.getElementsByTagName('event')[0].firstChild.data;

}

The error happens when I try to assign the value to the time variable. The firefox javascript console tells me that firstChild has no properties.

My question: how do I check to see if there is data present? I've tried several different things like
if(typeof(theXML.getElementsByTagName('time')[0].firstChild) != "undefined")
But it's just not working.

Thanks for your help.
 
Old December 29th, 2005, 05:16 PM
Registered User
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

if( theXML.getElementsByTagName('time')[0].firstChild ){
    var time = theXML.getElementsByTagName('time')[0].firstChild.data;
}







Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count all empty fields in Biztalk biztalkuser Biztalk 0 January 4th, 2006 07:37 AM
How to check for empty fields in a form. crmpicco Javascript How-To 7 February 15th, 2005 10:38 AM
empty fields not hidden anymore (php/mySQL) jasmin Beginning PHP 1 January 17th, 2005 12:42 PM
Empty Fields priority05 Access ASP 3 January 10th, 2005 04:40 PM
Subreport for CR9 giving empty fields in reports yblee Crystal Reports 0 August 18th, 2004 10:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.