using the same XML response

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

using the same XML response

Post by sarris »

Hi there. I am having three dropdown select boxes which are populated by taking some data from an XML file
The XML file has Continents, and every continent has Countries and every country has Cities.

The first select box gets populated with all the Continents, when the user selects a continent the second select box gets populated accordingly with the countries and when a country is selected the third select box is populated with the cities.

The thing is that with the knowledge i have on AJAX so far i make an XMLHttpRequest each and every time, getting the whole response every time.


What would be the way to use the first response to go through the XML document any time needed?

Code: Select all

function populate_list(){

	var url = "data.xml";
	var http_request = getHTTPObject();
        http_request.open("GET", url, true);
        http_request.onreadystatechange = function() {
               if(http_request.readyState == 4) {
                    if(http_request.status == 200){
						GetContinents(http_request);
                   }
            }
	}
       http_request.send(null);
}

function GetContinents(http_request){

	
	var response  = http_request.responseXML;
	var root = response.documentElement;
	var cities_list = root.getElementsByTagName("City");
	var N = cities_list.length;
	
	for(i=0;i<N;i++){
		tempCont = cities_list[i];
		tempname=tempCont.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		tempvalue=tempCont.getElementsByTagName("areafile")[0].childNodes[0].nodeValue;
		addOption(document.getElementById("select_cont"),tempname,tempvalue);
	}
	
	http_request.abort(); 
}
This is how i get my data to the continent selectbox and i have a couple of similar(almost identical) functions for populating each of the other selectboxes which are called onchange.

Having

Code: Select all

http_request = getHTTPObject();
to be a global variable and using that is too bad as a solution??

Thanks for any opinions you can offer.
Post Reply