tags:

views:

52

answers:

3

This works in all browsers except IE. I have no idea why. Its just an ajax clock that ticks through the seconds.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Time Program Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;

<script type="text/javascript">
$(document).ready( function(){
   zone = 'Africa/Djibouti';
   ajaxTimeCall(zone, ajaxCallback);
         });
function ajaxTimeCall(zone, callback) {
$.ajax({
     type: 'get',
     url: 'time.php',
     context: this,
     data: 'location=' + zone,
     success: function (data){
     var dataSplit = data.split(".");
     var isDST = dataSplit[1];
     var newTime = new Date(dataSplit[0]);
     //console.log(newTime);
     if(isNaN(newTime))
      {
      $('#clock_input').val("Timezone Not Recognized");
      return;
      }
     $('#clock_input').val(newTime);
     callback(isDST);
     }

  }); 
}
function ajaxCallback(isDST) {

  setTimeout("ajaxTimeCall(zone, ajaxCallback)", 1000);
  if (isDST == 1)
   $('.DST').removeClass('noshow');
}
</script>
<style type="text/css">
.noshow { display: none;}
.DST {color: red; font-size: 14px;}
</style>
</head>

<body>
<h1> World Time Ajax Testing</h1>
<span class="DST noshow">(DST)</span>
<form name="test" action="">
<input id="clock_input" style="width: 175px; font-size: 14px;" type="text" value="Turn on Javascript" />
</form>
</body>
</html>
A: 

Shouldn't your code be:

ajaxCallback(isDST);

in your ajax success callback in the function ajaxTimeCall?

If you could provide more details about the error that you're seeing, that would also help.

David Hoerster
Sorry about that I guess it would be helpful to know what I'm seeing. It displays an incorrect time in an incorrect format in IE. The correct time can be seen if you view it in FF or Chrome or Safari. It seems like its getting a different value for data back from the server than the other browsers. I have no idea why the format is different though.Also I think the callback name is correct since I pass the ajaxCallback() function into that callback name when I call ajaxTimeCall in the document.ready section? Is that incorrect?
gmills82
+1  A: 

UPDATE! http://stackoverflow.com/questions/592830/wtf-ie7-ajax-calls-using-settimeout This post contained the answer to my question. IE uses cached versions of GET variables and so the time was never being updated upon each AJAX call of the php script. I changed it to POST and it works like a charm. Thanks for all the help guys!

gmills82
check this as answer :)
Mark Schultheiss
A further update: Someone mentioned using the cache option in the ajax call from jquery. This was what I ended up going with. I switched the Post back to GET and turned cache to false and it worked. I also played with setting the cache options in the php headers.
gmills82
+1  A: 

I know you've already solved this with a POST, but there is another way as well, use the cache option for $.ajax(). By setting it to false, jquery will append a timestamp to the URL as a parameter, forcing a new request even in IE, you use it like this:

$.ajax({
  cache: false,
  //current options you already have...
});
Nick Craver