views:

346

answers:

1

I am facing problem for reading xml file using javascript.

It works fine in other browser but in it its throw the following error.

Microsoft JScript runtime error: 'null' is null or not an object

My code snippet is as below.

  GDownloadUrl("<?php echo $cfg->webroot;?>/G-map/map_xml/<?php echo $_SESSION['xml_file_name'];?>", function(data) {
              var xml = GXml.parse(data);
              var markers = xml.documentElement.getElementsByTagName("marker");

          for (var i = 1; i <=markers.length; i++) 
          { 

          /// Error produce here just for IE 

              var type=markers[i].getAttribute("type");
               var name =markers[i].getAttribute("title");

               var address =markers[i].getAttribute("address");
        //       var link= '<a href="doc_detail/doc_detail-'+ markers[i].getAttribute('id')+'.html" class="doc_url">Read More</a>';
               var point = new GLatLng(parseFloat(markers[i].getAttribute("latitude")),
               parseFloat(markers[i].getAttribute("longitude")));

               var marker = createMarker(point, name, address, type);
               map.addOverlay(marker);
          }

Thanks in advance

+1  A: 

Your loop is using a 1-based array: I'd be surprised if this was correct...

Try this instead:

for (var i = 0; i < markers.length; i++)
Greg
thanks a lot, its works for me, can you please explain whats the problem actually?
Avinash
Arrays in javascript are 0 based. This means that an array with 5 elements (length 5) has elements 0 through 4. Not 1 through 5. This means your condition was an "off by one" error.
coderjoe
Yup, there's not really anything to add to @coderjoe's explanation
Greg
Hi Greg, above code is my xml file read code using google api.It works fine in all browser, but in ie7 and ie8 it create a problem with reading xml file,Actually my xml file is updating on certain event and after that event this xml file is read through ajax and data will be displayed in map.Here xml file is updating very fine, but in ie7 an ie8, every time ajax gives the very first response.Means its not reading the latest xml file. Can you please explain what can be the problem, particular for ie7, and ie8..
Avinash
It sounds like the XML is being cached - add a random number to the URL - for example if you have "http://google.com/foo.xml" make it "http://google.com/foo.xml?rnd=" + Math.random()
Greg
Yes i found it from google,But hey Greg there is still javascript error for (var i = 1; i <=markers.length; i++)and for (var i = 0; i <=markers.length; i++) also.its throwing "Microsoft JScript runtime error: 'null' is null or not an object" error only in ie, but the script working fine after throwing error.. and in other bPlease help can't figure out whats the problem..every value from the xml file is reading very fine but don't know whats the problem...
Avinash