Services Products Purchase Free Trial Partner Support Company Contact LoopIP Home
 


Tutorial 8

 
 

NRS Tutorial: Using the XMLHttpRequest Object

Client-side Javascript Directory







JAVASCRIPT CODE USED:
// global flag
var isIE = false;
var pageurl = "http://demo.loopip.com/dir_tut8.xml?p="

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            displayInfo();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

// loads chosen XML document
function loadDoc(categoryid) {
    try {
        loadXMLDoc(pageurl + categoryid);
    }
    catch(e) {
    var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
    alert("Unable to get XML data:\n" + msg);
    return;
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

function displayInfo() {

	var  categories = req.responseXML.getElementsByTagName("child");
	var cathtml = "";
	var i;
	for (var i = 0; i < categories.length; i++) {
	var childtype = getElementTextNS("", "type", categories[i], 0);
	if (childtype == 'main' || 
		childtype == 'maintop' || 
		childtype == 'normal' || 
		childtype == 'letterbar') 
	{
	cathtml = cathtml + "<a href='#' onclick='javascript:loadDoc(";
	cathtml = cathtml + getElementTextNS("", "id", categories[i], 0) + ")'>";
	cathtml = cathtml + getElementTextNS("", "name", categories[i], 0);
	cathtml = cathtml + "</a><br>";
	}
	}
	document.getElementById("categories").innerHTML = cathtml;

	var  parents = req.responseXML.getElementsByTagName("parent");
	var parenthtml = "";
	var cat = req.responseXML.getElementsByTagName("cat");
	var catid = getElementTextNS("", "id", cat[0], 0);
	var catname = getElementTextNS("", "name", cat[0], 0);
	if (catid != 1)
	parenthtml = "<a href='#' onclick='javascript:loadDoc(1)'>Top</a> : ";
	for (i = 0; i < parents.length; i++) {
	parenthtml = parenthtml + "<a href='#' onclick='javascript:loadDoc(";
	parenthtml = parenthtml + getElementTextNS("", "id", parents[i], 0) + ")'>";
	parenthtml = parenthtml + getElementTextNS("", "name", parents[i], 0);
	parenthtml = parenthtml + "</a> : ";
	}
	if (catid != 1)
	{
	parenthtml = parenthtml + "<a href='#' onclick='javascript:loadDoc(";
	parenthtml = parenthtml + catid + ")'>";
	parenthtml = parenthtml + catname + "</a>";
	}
	document.getElementById("parents").innerHTML = parenthtml;

	var sites = req.responseXML.getElementsByTagName("site");
	var sitehtml = "";
	for (i = 0; i < sites.length; i++) {
	sitehtml = sitehtml + "<a href='http://";
	sitehtml = sitehtml + getElementTextNS("", "url", sites[i], 0) + "'>";
	sitehtml = sitehtml + getElementTextNS("", "name", sites[i], 0);
	sitehtml = sitehtml + "</a><br>";
	sitedesc = getElementTextNS("", "desc", sites[i], 0);
	if (sitedesc != '')
		sitehtml = sitehtml + sitedesc + "<br>";
	}
	document.getElementById("details").innerHTML = sitehtml;
}
								
 
 
Back to support
 
 
 
LoopIP search
Web search
Net Research Server
Net Research Server - demonstration website
visit Net Research Server - demonstration website
 
Search Example
Application Example 
 

© 2007 LoopIP LLC. All rights reserved | Terms | Privacy