Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure why this won't update in IE. 2

Status
Not open for further replies.

herrin

Programmer
Nov 27, 2001
12
US
Hi there, I have a quick question.

This script is fairly simple. It works great in firefox, doesn't seem to work in IE. My document.getElementById('theTime').value gets set in firefox, but not in IE.

The php script returns a simple time string with the current server time, like March 10, 2005 11:15am, for example.

In firefox, the time updates every 5 seconds as I would expect.

In IE the time never changes and remains at the first value retrieved from the server. I'm returning no-cache headers from the php script.

Anyone have any ideas?


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head>
<title>XmlHttpRequest</title>

<script language="javascript" type="text/javascript">
  var url = "echotime.php?param=";
  var http = getHTTPObject();
  
  function startAjax()
  {
    getTime();
    setInterval('getTime();', 5000);
  }
  
  function handleHttpResponse()
  {
    if (http.readyState == 4)
    {
   if (http.status == 200)
   {
         document.getElementById('theTime').value = http.responseText;
   }
   else
   {
   alert("There was a problem retrieving the XML data:\n" + http.statusText);
   }  
    }
  }
  
  function getTime()
  {
    http.onreadystatechange = handleHttpResponse;
    http.open("GET", url, true);
    http.send(null);
  }
  
  function getHTTPObject()
  {
   if (window.XMLHttpRequest)
   {
      try
      {
        xmlhttp = new XMLHttpRequest();
      }
      catch (e)
      {
        xmlhttp = false;
      }
   }
   else if (window.ActiveXObject)
   {
      try
      {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        window.alert("ms.xmlhttp");
        try
        {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
          xmlhttp = false;
        }
      }
   }
   return xmlhttp;
  }
</script>

</head>
<body onload="startAjax();">

<form action="post" ID="theForm">
  <label for="time">time</label>
  <input type="text" name="test" id="theTime" width="200px" size="200px" value="crap"/>
</form>
</body>
</html>
 
It seems that my XMLhttprequest stuff is working fine. I browsed that link and it looks like i'm doing it correctly. This is just something IE specific. Thanks for the reply.
 

Have you checked your browser's cache settings? They may be set to never check the pages coming from the server for newer versions.

Dan


The answers you get are only as good as the information you give!

 
Thanks for replying. I did check the cache and even set it to check for a new version every time.
 
Using IE, try putting an alert in after this line:

Code:
if (http.status == 200)
{

Just so you can confirm that it is actually getting there more than just the once.

Hope this helps,
Dan


The answers you get are only as good as the information you give!

 
Hey guys/gals, I can't tell you how much I appreciate the responses.

I did add an alert there and it is being triggered every 5 seconds, but the value of the input is still not updated. I'm frickin' stumped.
 

Do you have a URL where we can load echotime.php from?

Dan


The answers you get are only as good as the information you give!

 

This is very strange. It won't work at all for me in Firefox, stopping with an error in the JS console that says:

Error: uncaught exception: Permission denied to call method XMLHttpRequest.open

And in IE, the setInterval does NOT seem to be running at all - only the initial call to getTime works!

I'll keep working away at it, but I've never seen a call to setInterval fail.

Dan


The answers you get are only as good as the information you give!

 
OK - The Firefox error was due to me accessing a page in your domain from my local machine.

As for IE - If you add the following as the first line of getTime():

Code:
http = getHTTPObject();

then it works perfectly:

Code:
	<script type="text/javascript">
	<!--
		var url = "[URL unfurl="true"]http://viewwerx.com/echotime.php?param=";[/URL]
		var http;

		function startAjax() {
			getTime();
			setInterval('getTime();', 5000);
		}

		function handleHttpResponse() {
			if (http.readyState == 4) {
				if (http.status == 200) {
					document.getElementById('theTime').value = http.responseText;
				} else {
					alert("There was a problem retrieving the XML data:\n" + http.statusText);
				}
			}
		}

		function getTime() {
			http = getHTTPObject();
			http.onreadystatechange = handleHttpResponse;
			http.open("GET", url, true);
			http.send(null);
		}

		function getHTTPObject() {
			if (window.XMLHttpRequest) {
				try {
					xmlhttp = new XMLHttpRequest();
				}
				catch (e) {
					xmlhttp = false;
				}
			} else if (window.ActiveXObject) {
				try {
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					window.alert("ms.xmlhttp");
					try {
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (E) {
						xmlhttp = false;
					}
				}
			}
			return xmlhttp;
		}
	//-->
	</script>

Hope this helps,
Dan




The answers you get are only as good as the information you give!
 
You know man, I love that fact that people take the time to help others out.

Your fix worked perfectly and I greatly appreciate the time and effort that you put in to provide me with the advice.

I actually thought about that answer the other night that the getHTTPObject was only getting called and runtime and the object wasn't getting refreshed, but I forgot to follow up on it.

Once again, thanks!
 

Good to see it worked for you too - does it still work in FF? ;o)

Dan


The answers you get are only as good as the information you give!

 
Good work, Dan. Your "Tip Master of the Week" title is well deserved! [smile]

Adam

recursion (n.) See recursion.
 
Yep, works great in both browsers. Thanks again!
 
Hello Dan,

I concur that adding http = getHTTPObject() within the getTime() function just before http.open() make the script work for both ie & ff. But, I have one objection, and I have one solution that I'm happier with.

The http is global in scope. Hence, reset another instance of http like that means it has got lost somehow. This is a severe lost if it really is the case.

In fact, [1] if you inspect the hppt just before .open() line, it preserves its standing as object. [2] If you inspect the readyState after the .send(null) line, you'll its readyState does change to 1 instead of 4. Hence, in these two aspects http is okay.

The only problem in IE is that the handler handleHttpResponse never got called. This is the single real trouble which causes the breakdown of functionality.

The solution I propose is move the assignment of the handler after the http.open(), like this.
Code:
  function getTime()
  {
    //http.onreadystatechange = handleHttpResponse;
    http.open("GET", url, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
  }
or this --- anywhere after the .open().
Code:
  function getTime()
  {
    //http.onreadystatechange = handleHttpResponse;
    http.open("GET", url, true);
    http.send(null);
    http.onreadystatechange = handleHttpResponse;
  }
It looks like in IE, each time ixmlhttp object "open", a "session" in its life-cycle is born anew making it lost the memory of the onreadystate handler. (This is my interpretation.)

I understand that a lot of code outthere in the wild use the same sequence (handler assignment, open, send). So, I cannot object those in general. My study through points out in this particular functionality, it seems to suggest this interpretation for ie.

regards - tsuji

 

Good point, tsuji. I must admit to not looking into the possibility of simply resetting the object properties, etc, rather than re-instantiating it.

I think the reason for this was that I was fairly certain that the "garbage collection" (that's such an American phrase!) within JavaScript would have removed all traces of the old object before creating the new one.

Dan


The answers you get are only as good as the information you give!

 
Thanks again guys! I did move it back outside the function and moved the onreadystatechange. That's kind of funky.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top