作者sicao (adios)
看板Web_Design
標題[問題] AJAX讀檔自動更新網頁
時間Tue Dec 14 14:55:47 2010
底下的程式碼是在網路上抄來的
在Firefox上
只要更新autoupdate.txt內容就會更新
但Chrome反應就沒那麼快 要等一陣子才會更新
IE卻一直緊咬舊資料不更新
即使重新整理也一樣
請問我該怎麼修正??
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<META HTTP-EQUIV="expires" CONTENT="0">
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<html>
<head>
<title></title>
<script type="text/javascript" language="javascript">
var http_request = false;
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', 'autoupdate.txt', true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById("up").innerHTML = "";
document.getElementById("up").innerHTML = http_request.responseText;
//alert('1');
//alert(http_request.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
function MyShow() {
//2秒自?刷新一次,2秒取得一次?据.
timer = window.setInterval("makeRequest()",2000);
}
</script>
</head>
<body onload="MyShow()">
<span
style="cursor: pointer; text-decoration: underline"
onclick="makeRequest('autoupdate.txt')">
Make a request
</span>
<div id="up"></div>
</body>
</html>
--
5L36f6d6I5207573K657273206f6E62066Y697265O666f78206172U65207375636b2e
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.78.167.138
→ opoepev:這是ie本身的問題~要按ctrl+f5才有用...除非程式下reload 12/14 16:14
→ s25g5d4:.txt 的url後面加個時間 12/14 20:24
→ s25g5d4:另外 蠻好玩的是你function有傳url進去 但是function卻寫 12/14 20:24
→ s25g5d4:死URL... 12/14 20:25
→ foreverangle:new Date 12/14 22:09