Understanding a function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Understanding a function

Post by gautamz07 »

below is the code :

Code: Select all

  function createXHR()
		{
			try { return new XMLHttpRequest(); } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
			alert("XMLHttpRequest not supported");
			return null;
		}
		function sendRequest()
		{
			var xhr = createXHR(); // cross browser XHR creation
				if (xhr) // if created run request
				{
					xhr.open("GET","sayhello.php",true);
					xhr.onreadystatechange = function(){handleResponse(xhr);};
					xhr.send(null);
				}
		}

		function handleResponse(xhr){
			if (xhr.readyState == 4 && xhr.status == 200)
			{
			var parsedResponse = xhr.responseXML;
			var msg = parsedResponse.getElementsByTagName("message")[0].firstChild.nodeValue;
			var responseOutput = document.getElementById("responseOutput");
			responseOutput.innerHTML = msg;
			}
		}

		window.onload = function ()
		{
		document.getElementById("helloButton").onclick = sendRequest;
		};
I have a problem understanding this function , check the code .

Code: Select all

     		function handleResponse(xhr){
			if (xhr.readyState == 4 && xhr.status == 200)
			{
			var parsedResponse = xhr.responseXML;
			var msg = parsedResponse.getElementsByTagName("message")[0].firstChild.nodeValue;
			var responseOutput = document.getElementById("responseOutput");
			responseOutput.innerHTML = msg;
			}
		}
i Don't get what the below line is doing in the code ? , is it an absolute must ???

Code: Select all

   var parsedResponse = xhr.responseXML;
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Understanding a function

Post by Celauran »

It's saving a property of the xhr object to a local variable.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Understanding a function

Post by twinedev »

More specifically, it is taking the response from the AJAX request and converting it to XML format and then assigning that to parsedResponse
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Understanding a function

Post by gautamz07 »

twinedev wrote:More specifically, it is taking the response from the AJAX request and converting it to XML format and then assigning that to parsedResponse
Why is it converting it to XML ????
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Understanding a function

Post by gautamz07 »

Or is it converting The PHP response to XML or the JS to XML .. I am confused :o
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Understanding a function

Post by twinedev »

Why? Well that is just what the function does. I have never used it before, but my guess would be that the PHP output (the response being processed) is formatted at XML code, and by converting into an actual XML object, that is what lets you used the data given with XML functionality.

Like I said, I don't mess with XML much at all, so someone else may be able to elaborate more. Myself, for scripts designed to work with AJAX, I set the output to be JSON
Post Reply