Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript functions 1

Status
Not open for further replies.

comissioner

Technical User
Apr 7, 2009
2
IE
Hello to all!
Im new to js and ive run into difficulty
I have a html page with 2 parts of javascript.
1. this is to retrieve text
2. This is to retrieve data from an xml file

I have 2 functions in an external js file but the problem is only 1. works
Im using a xmlhttpRequest.

Here is the js function code
Code:
function loadScript(scriptURL)
{
	var newScript = document.createElement("script");
	newScript.src = scriptURL;
	document.body.appendChild(newScript);
}

function createXMLHttpRequest() {
	xmlReq = null;
	if(window.XMLHttpRequest) 		xmlReq = new XMLHttpRequest();
	else if(window.ActiveXObject) 	xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	if(xmlReq==null) return; // Failed to create the request
}

function loadData(URL)
{
// Anonymous function to handle changed request states
	xmlReq.onreadystatechange = loadData(URL)
	{
		switch(xmlReq.readyState)
		{
		case 0:	// Uninitialized
			break;
		case 1: // Loading
			break;
		case 2: // Loaded
			break;
		case 3: // Interactive
			break;
		case 4:	// Done!
		// Retrieve the data between the <quote> tags
			doSomethingWithData(xmlReq.responseXML.getElementsByTagName('quote')[0].firstChild.data);
			break;
		default:
			break;
		}
	}

// Make the request
	xmlReq.open ('GET', URL, true);
	xmlReq.send (null);
}

function loadXML(url)
{
	xmlReq2 = null;
	if(window.XMLHttpRequest) 		xmlReq2 = new XMLHttpRequest();
	else if(window.ActiveXObject) 	xmlReq2 = new ActiveXObject("Microsoft.XMLHTTP");
	if(xmlReq2==null) return; // Failed to create the request


// Anonymous function to handle changed request states
	xmlReq2.onreadystatechange = loadXML()
	{
		switch(xmlReq2.readyState)
		{
		case 0:	// Uninitialized
			break;
		case 1: // Loading
			break;
		case 2: // Loaded
			break;
		case 3: // Interactive
			break;
		case 4:	// Done!
		// Retrieve the data between the <quote> tags
			doSomethingWithXml(xmlReq2.responseXML.getElementsByTagName('quote')[0].firstChild.data);
			break;
		default:
			break;
		}
	}

// Make the request
	xmlReq2.open ('GET', URL, true);
	xmlReq2.send (null);
}
Could some one help me out? Ive literally spent hours on this and I cant get it to work properly. Each part works fine on their own but when I try to put them together i have difficulties.
Any help would be greatly appreciated!
 
function loadXML(url)
{
xmlReq2 = null;
if(window.XMLHttpRequest) xmlReq2 = new XMLHttpRequest();
else if(window.ActiveXObject) xmlReq2 = new ActiveXObject("Microsoft.XMLHTTP");
if(xmlReq2==null) return; // Failed to create the request


// Anonymous function to handle changed request states
xmlReq2.onreadystatechange = loadXML()
You are assigning to the readystate handler, the return value of the function that creates it. This is going to cause infinite recursion.

Code:
xmlReq2.onreadystatechange = function()
{
 .........
}
Also the switch-case constuct is pointless here.
 
Thank you Clueful!
It works perfectly now. I really can thank you enough for your help.!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top