I'd like to convert a working javascript function that uses XMLHttpRequest() (or ActiveXObject, if necessary) to perform a synchronous data fetch from the server into an asynchronous one, if it can be done without undue pain. This function today "caches" the data so received, so it only needs to be fetched once. When I do the "simple" thing of merely selecting the asynchronous option of XMLHttpRequest() to perform the fetch asynchronously using the provided-for call-back feature, callers to my function don't get their data because it typically hasn't been returned yet from the server, and so the moment is lost. After the data has returned, of course there's no problem, but since many calls must come from onload, they aren't easily repeated. (Is there a way to "fire" all the functions in an inload list after the load as completed? This might be an alternative way around the issue.)
When, instead, it's done synchronously, the system works fine. There's a slight delay for some things until the data returns, but it's not enough, typically, to be an issue (so far). However, this usage is deprecated. Firefox's developer's console, for example, reports:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/
Since I don't really know any other mechanism (which is what this question is all about), I was thinking that I'd have to make my function that calls XMLHttpRequest() somehow track the calls it receives from other functions and take on the responsibility of performing whatever they need done after data has been received. Calling functions could, perhaps, be broken into two parts, one that requests the data, another that gets done whatever is needed when the data has shown up. The function using XMLHttpRequest() could implement this, for example, by adding a number of arguments to its function signature, such as what function to later call and so forth. ... I think it could be done but it's an unweildy mess. There must be a better way, I'm just trying to come up with one.
To be clear, the code works as synchronous right now. I'm looking for an easier way to make it asynchronous. It doesn't seem that easy!
Here's a snippet of the code that does it synchronously:
var xmlhttp;
var dn = window.location.hostname;
var srcURL = "http://"+dn+"/unpublished/latest/this.txt";
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", srcURL, false);
xmlhttp.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
xmlhttp.send("fname=Who&lname=Cares");
if (xmlhttp.readyState == 4 && xmlhttp.status==200)
{
var siteData = xmlhttp.responseText;
. . .
The asynchronous version - that works fine in fetching the data, but too late for some callers - is rather similar. Here's a snippet of it:
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//the callback function to be called when AJAX request comes back
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status==200)
{
var siteData = xmlhttp.responseText;
... etc ...
Here's an example of what the callers to look like. Note that getSiteData()
is the function that contains the snippets above. To wit:
<script type = "text/javascript">
function setLinkToContact(objId)
{
document.getElementById(objId).setAttribute('href',
getSiteData('contact'));
}
</script>
There are MANY such callers for one "getSiteData()"
function.