I have an XHR call getting a date for me, but can't seem to pass it into the page. I just need to pass the date from the XHR call to a variable to be inserted via document.write
.
Here is my code:
var upDated
function getUpdated(){
xmlhttp.open("HEAD", "MBP_box.JPG",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
upDated = xmlhttp.getResponseHeader("Last-Modified");
alert(upDated);
}
}
xmlhttp.send(null)
}
and in the body....
document.write(upDated);
Instead of alert(upDated); you should have something like:
document.getElementById("some-element").innerHTML = upDated;
The idea is pretty simple, you must keep the logic inside the callback function (the one assigned to onreadystatechange) as you have no idea when it will be called (it will be called when the browser has received some response from the server, which may just as well not happen). So having some code after you set this connection won't work.
I'm not sure I understand what you want - do you mean you want the value of upDated, as returned via the Ajax call, to be inserted into the document body via document.write
?
If so, you definitely can't do this. document.write
is executed as soon as it is encountered, which will be while the document is loading. If you want to inject a value dynamically, you will have to use a DOM method.