views:

78

answers:

2

I have an XHR call getting a date for me, but can't seem to pass it into the page - i know its school-boy stuff, but its driving me mad.

I just need to pass the date from the XHR call to a variable to be inserted via document.write...

Anyone ?

thanks in advance.

B


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);
+3  A: 

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.

Ionuț G. Stan
dammit. yes of course.im mixing old and new. (my JS is very sketchy..)this makes perfect sense now. :)
Working.many thanks :)http://swind.co.uk/_bin/TEST/XHR_TEST
A: 

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.

Daniel Roseman
or use .innerHTML as lonut did. (note that innerHTML is not part of any DOM standard)
Jonathan Fingland
"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?"yes exactly that.i think ive got it. - its my stupid old JS brain.. :)need to to use a DOM insert instead.