四大步骤:
1、抽出要刷新局部代码到一个新的jsp,加一个ID 属性
<div id="freshTable">
//抽出的部分代码
</div>
2、写ajax 刷新的js
function makeRequest(url) {
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType){
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) {
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
if (!http_request) {
alert("您的浏览器不支持当前操作,请使用 IE 5.0 以上版本!");
return false;
}
//定义页面调用的方法init,不是init();没有();
http_request.onreadystatechange = init;
http_request.open('GET', url, true);
//禁止IE缓存
http_request.setRequestHeader("If-Modified-Since","0");
//发送数据
http_request.send(null);
//每30秒刷新一次页面
timebs=setTimeout("makeRequest('"+url+"')", 5000);
}
function init() {
if (http_request.readyState == 4) {
if (http_request.status == 0 || http_request.status == 200) {
var result = http_request.responseText;
if(result==""){
result = "信息获取失败";
}
document.getElementById("freshTable").innerHTML=result;
} else {//http_request.status != 200
alert("请求失败!");
}
}
}
3、在该页面调用抽出的jsp(写一个div 通过ID调用)
<div id="freshTable">
</div>
4、在该页面写js调用刷新的方法
<script type="text/javascript">
window.onload = Function(makeRequest('<%=basePath%>/fresh.jsp'));
</script>