View Single Post
Old 11-07-2012, 04:21 AM   PM User | #1
hosker
New to the CF scene

 
Join Date: Oct 2012
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
hosker is an unknown quantity at this point
Using AJAX to read an XML file

I am new to using AJAX and am struggling in make the following code work:

Code:
Core.sendRequest = function(url,callback,postData) {
	//checks to see if we can create the XMLHttpObject
	var req = Core.createXMLHttpObject();
	
	//if returns false cancel operation 
	if (!req) return;
	
	//check to see if postData was passed if so set method to POST
	var method = (postData) ? "POST" : "GET";
	
	//call the open method, send the method "POST" or "GET" and pass true
	req.open(method,url,true);
	
	//set the request header
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	
	//if postData is sent then set request header for forms
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	//if everything returns ok send req value to "callback"
	req.onreadystatechange = function () {
		if (req.readyState !== 4) return;
		if (req.status !== 200 && req.status !== 304) {
			return;
		}
		callback(req);
	}
	// if we have already completed the request, stop the function so as not
  	// to send it again
	if (req.readyState === 4) return;
	
	//if postdata was included send it to server side page. Information
	//can be received by using $_POST['data'] (this is via PHP)
	if (postData){
		req.send("data="+postData);
	}
	else{
		req.send(null);
	}
	
}

//depending on the browser return appropriate request.
Core.XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

//This method cycles through all requests in XMLHttpFactories until
//one is found.
Core.createXMLHttpObject = function() {
	var xmlhttp = false;
	for (var i=0;i<Core.XMLHttpFactories.length;i++) {
		try {
			xmlhttp = Core.XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
Second js file:

Code:

var Core = {};

// W3C DOM 2 Events Model
if (document.addEventListener)
{

	Core.addEventListener = function(target, type, listener)
	{
    	target.addEventListener(type, listener, false);
	};
	
	Core.stopPropagation = function(event)
  	{
    	event.stopPropagation();
  	};
  	Core.preventDefault = function(event)
  	{
    	event.preventDefault();
  	};
	Core.removeEventListener = function(target, type, listener)
  	{
    	target.removeEventListener(type, listener, false);
  	};

}

else if (document.attachEvent)
{
  Core.addEventListener = function(target, type, listener)
  {
    // prevent adding the same listener twice, since DOM 2 Events ignores
    // duplicates like this
    if (Core._findListener(target, type, listener) != -1) return;

    // listener2 calls listener as a method of target in one of two ways,
    // depending on what this version of IE supports, and passes it the global
    // event object as an argument
    var listener2 = function()
    {
      var event = window.event;

      if (Function.prototype.call)
      {
        listener.call(target, event);
      }
      else
      {
        target._currentListener = listener;
        target._currentListener(event)
        target._currentListener = null;
      }
    };

    // add listener2 using IE's attachEvent method
    target.attachEvent("on" + type, listener2);
    
    //The above code allows us to create an event listener for IE
    //and be able to use the "this" keyword.  The code below stores 
    //our object references so the can be cleaned up later.  This 
    //stops any memory leak issues.
    

    // create an object describing this listener so we can clean it up later
    var listenerRecord =
    {
      target: target,
      type: type,
      listener: listener,
      listener2: listener2
    };

    // get a reference to the window object containing target
    var targetDocument = target.document || target;
    var targetWindow = targetDocument.parentWindow;

    // create a unique ID for this listener
    var listenerId = "l" + Core._listenerCounter++;

    // store a record of this listener in the window object
    if (!targetWindow._allListeners) targetWindow._allListeners = {};
    targetWindow._allListeners[listenerId] = listenerRecord;

    // store this listener's ID in target
    if (!target._listeners) target._listeners = [];
    target._listeners[target._listeners.length] = listenerId;

    // set up Core._removeAllListeners to clean up all listeners on unload
    if (!targetWindow._unloadListenerAdded)
    {
      targetWindow._unloadListenerAdded = true;
      targetWindow.attachEvent("onunload", Core._removeAllListeners);
    }
  };
  
Core.stopPropagation = function(event)
  {
    event.cancelBubble = true;
  };
  
Core.preventDefault = function(event)
  {
    event.returnValue = false;
  };
  
Core.removeEventListener = function(target, type, listener)
  {
    // find out if the listener was actually added to target
    var listenerIndex = Core._findListener(target, type, listener);
    if (listenerIndex == -1) return;

    // get a reference to the window object containing target
    var targetDocument = target.document || target;
    var targetWindow = targetDocument.parentWindow;

   	// obtain the record of the listener from the window object
    var listenerId = target._listeners[listenerIndex];
    var listenerRecord = targetWindow._allListeners[listenerId];

    // remove the listener, and remove its ID from target
    target.detachEvent("on" + type, listenerRecord.listener2);
    target._listeners.splice(listenerIndex, 1);

    //remove the record of the listener from the window object
    delete targetWindow._allListeners[listenerId];
  };
    

Core._findListener = function(target, type, listener)
  {
    // get the array of listener IDs added to target
    var listeners = target._listeners;
    if (!listeners) return -1;

    // get a reference to the window object containing target
    var targetDocument = target.document || target;
    var targetWindow = targetDocument.parentWindow;

    // searching backward (to speed up onunload processing), find the listener
    var len = listeners.length;
    for (var i = len - 1; i >= 0; i--)
    {
      // get the listener's ID from target
      var listenerId = listeners[i];

      // get the record of the listener from the window object
      var listenerRecord = targetWindow._allListeners[listenerId];

      // compare type and listener with the retrieved record
      if (listenerRecord.type == type && listenerRecord.listener == listener)
      {
        return i;
      }
    }
    return -1;
  };

  Core._removeAllListeners = function()
  {
    var targetWindow = this;
	//this is a for loop going though an associative array
    for (id in targetWindow._allListeners)
    {
      var listenerRecord = targetWindow._allListeners[id];
      listenerRecord.target.detachEvent("on" + listenerRecord.type, listenerRecord.listener2);
      delete targetWindow._allListeners[id];
    }
  };

  Core._listenerCounter = 0;
}//else if (document.attachEvent)
The client would like it to read an XML file by using an input box to type in your query and display the results.

The XML file is basic an I need to search it via the authors name. On some of them, there are two authors listed please let me know you canl I always want
hosker is offline   Reply With Quote