JSP+Ajax div自动刷新

[ 2007-08-14 13:37:59 | Author: zhenhua ]
Font Size: Large | Medium | Small
无刷新自动更新div里的数据

ajax.htm:
<script language="javascript">
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "auto.jsp";
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
DisplayHot();
setTimeout("sendRequest()", 60000);
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
function DisplayHot()
{
var theDate = XMLHttpReq.responseText ;//如果出现编码问题,可以在服务端escape一下,然后在这里使用unescape( responseText )
theDiv.innerHTML = theDate ;
}
</script>
<div id="theDiv"></div>
auto.jsp:
<%@ page contentType="text/html; charset=UTF-8" %>
<%
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
out.println("<response>");
for(int i=0;i<2;i++){
out.println("<name>"+(int)(Math.random()*10)+"型笔记本</name>");
out.println("<count>" +(int)(Math.random()*100)+ "</count>");
}
out.println("</response>");
out.close();
%>

auto.jsp如果出现异常,请在"out.close(); " 后加一句 return;

演示:http://www.chaowaihui.cn
首页今日操作

===================
一、JSP文件:auto.jsp
<%@ page contentType="text/html; charset=gb2312" %>

<%
//设置输出信息的格式及字符集
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
out.println("<response>");

for(int i=0;i<2;i++){
out.println("<name>"+(int)(Math.random()*10)+"型笔记本</name>");
out.println("<count>" +(int)(Math.random()*100)+ "</count>");
}
out.println("</response>");
out.close();
//www.wnetw.net

%>
 

二、html文件:autoRefresh.html
<head>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
</head>
<script language="javascript">
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "auto.jsp";
XMLHttpReq.open("GET", url, true);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null); // 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
DisplayHot();
setTimeout("sendRequest()", 1000);
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
function DisplayHot() {
var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue;
document.getElementById("product").innerHTML = name;
document.getElementById("count").innerHTML = count;
}

</script>

<body onload ="sendRequest()">
<table style="BORDER-COLLAPSE: collapse" borderColor="#111111"
cellSpacing="0" cellPadding="0" width="200" bgColor="#f5efe7" border="0">
<TR>
<TD align=middle height=4 colspan="2"><IMG height=4
src="images/promo_list_top.gif" width="100%"
border=0>
</TD>
</TR>
<TR>
<TD align="center" bgColor="#dbc2b0"
height="19" colspan="2"><B>正在热卖的笔记本</B>
</TD>
</TR>
<tr>
<td height="20">
型号:
</td>
<td height="20" id="product">
</td>
</tr>
<tr>
<td height="20">
销售数量:
</td>
<td height="20" id="count">
</td>
</tr>
</body>
</table>
 

Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=477
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=477

View Mode: Show All | Comments: 1 | Trackbacks: 0 | Toggle Order | Views: 21287
1
Quote Ychon
[ 2007-08-14 13:54:56 ]
插入div
<script>
 function dd(){
 document.all("my").innerHTML="xxxxx"
 }
 </script>
 <input type="button" onclick="dd()"
 
 value="click">
 <br>
 <div id="my">yyyyyy</div>
1

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code