XML HTTP GET does not seem to work in IE6 when used with setTimeout

R

Robert S

I am trying to get the output of a php script to be displayed on a page
using the following (I have drastically simplified the PHP script and
have used MSXML2.XMLHTTP.4.0):

time.html:
<html>
<head>
<script type="text/javascript" language="javascript">
var xmlhttp;
function loadXMLDoc() {
xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.4.0");
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4)
if (xmlhttp.status==200) {
document.getElementById("refresher_div").innerHTML =
xmlhttp.responseText;
setTimeout("loadXMLDoc()", 1000);
}
};
xmlhttp.open("GET","time.php",true);
xmlhttp.send(null);
} else {
alert("Your browser does not support XMLHTTP.");
}
}
</script>
</head>

<body>
<center>
<div name="refresher_div" id="refresher_div">
Time will display here!!
</div>
</center>
</body>
</html>
<script language="javascript">
loadXMLDoc();
</script>
-----------------------------------8><-------------------------------------------
time.php:
<?php print date("r"); ?>
-----------------------------------8><-------------------------------------------
It all works fine in Firefox, but in IE6 and 5.0 the time does not
refresh on the page. Looking at the web server log, time.php does not
seem to load every second when it is running in IE6, and, using the
Script Debugger, the value of xmlhttp.responseText does not get
updated.

What am I doing wrong?

I've tried different versions of MS XMLHTTP, and have tried synchronous
GET statements, but no joy. I've tried debugging it with alert(
xmlhttp.responseText ), and it doesn't get updated. I've also tried
rearranging things, but the same fundamental problem remains.
 
M

Martin Honnen

Robert S wrote:

time.php:
<?php print date("r"); ?>
-----------------------------------8><-------------------------------------------

IE is probably caching the page so make sure your PHP script sends the
right HTTP headers to indicate that it should not be cached respectively
expires immediately.

On the client-side if you are sure you want to force a new request then
it could help to use a unique query string each time e.g.
xmlhttp.open("GET","time.php?time=" + new Date().getTime(),true);
 
R

Robert S

IE is probably caching the page so make sure your PHP script sends the
right HTTP headers to indicate that it should not be cached respectively
expires immediately.

On the client-side if you are sure you want to force a new request then
it could help to use a unique query string each time e.g.
xmlhttp.open("GET","time.php?time=" + new Date().getTime(),true);
I've done it this way - lifted from the php manual:

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
print date("r");
?>

The second suggestion also works.

If I run this on IE5.0, it starts skipping seconds after a while then
IE hangs. I assume that this is not fully supported.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top