This code only works when async is set to false, why?
var contact =
{
XMLHttp : null,
XMLDoc : null,
TXTDoc : null,
getData : function(dataSource)
{
contact.XMLHttp = new XMLHttpRequest();
contact.XMLHttp.open("GET", dataSource, false);
contact.XMLHttp.onreadystatechange = contact.storeData;
contact.XMLHttp.send(null);
},
storeData : function()
{
if(contact.XMLHttp.readyState == 4 && contact.XMLHttp.status == 200)
{
contact.XMLDoc = contact.XMLHttp.responseXML;
contact.TXTDoc = contact.XMLHttp.responseText;
}
},
displayData : function(elementID)
{
if(contact.TXTDoc != null)
document.getElementById(elementID).innerHTML = contact.TXTDoc;
else{
document.getElementById(elementID).innerHTML = "can't do it";
}
}
}
I import it into a html document like so:
<head> <script type="text/javascript" src="contact.js"></script> </head>
And use it like so:
<body id="para"> <script type="text/javascript"> contact.getData("http://localhost/~olatunjigbadamosi/Books/contact.txt"); contact.storeData(); contact.displayData("para"); </script>