开发者

Why doesn't this jQuery code work in IE? [duplicate]

This question already has answers here: Wtf IE7 - AJAX calls using setTimeout (4 answers) Closed 2 years ago.

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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"></script>

<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>


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...
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜