Understanding a function
Posted: Sun Sep 28, 2014 8:44 am
below is the code :
I have a problem understanding this function , check the code .
i Don't get what the below line is doing in the code ? , is it an absolute must ???
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;
};
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;
}
}
Code: Select all
var parsedResponse = xhr.responseXML;