我们在网页制作的过程中经常会遇到及时刷新数据的问题,如果使用 的方法,会造成整个屏幕不断闪烁刷新的效果,这会降低用户的操作满意度。
所以我们需要一种可以实现无闪自动刷新数据的方法来解决以上问题。
实例解决问题:
希望实现用户在进入系统以后(整个session的时效之内),如果收到新邮件则发出声音提示。
实现思路:
1.首页部分:< body onload="init('<%=ses_userBean.getUsername()%>');"> // load时调用init(user);
2.js部分:用XMLHTTP实现页面局部刷新,调用check_mail.jsp对后台数据库进行检索判断并返回结果。
<!--var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;
function init(user){
username=user;
window.setInterval('Checkmail()',5000);//每隔5秒自动调用Checkmail()
}
function Checkmail()
{
xmlhttp.open("POST", "check_mail.jsp?uName="+username, false);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.send();
}
function updatePage() {
if (xmlhttp.readyState < 4) {
test1.innerHTML="loading...";
}
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if(response==1){//判断为假
test1.innerHTML=" ";
checkresult=1;
}
else{//判断为真
test1.innerHTML="<ccid_file alt=新邮件 values="img/tp024"
alt=新邮件 src=img/tp024.gif />
<EMBED src='music/nudge.wma' hidden=true autostart=true loop=false>";
checkresult=0;
}
}
}
// -->
3.check_mail.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="error/login_error.jsp"%>
<%@ page import="myweb.*" %>
<%@ page import="java.sql.*" %>
<%
String user=request.getParameter("uName");
Connection conn=null;
try{
conn=DBConnection.getConnection();
PreparedStatement pStat=conn.divpareStatement("
select * from message where r_name='"+user+"' and status=0");
ResultSet rs=pStat.executeQuery();
if(rs.next()){//有记录
response.getWriter().print(0);
}else{
response.getWriter().print(1);
}
}finally{
if(conn!=null) conn.close();
}
%>
4.首页结果显示
将< span id="test1" > < /span >插入指定位置。文章地址: http://www.qqread.com/ajax/u327451.html
更多内容请看Ajax技术专题、Ajax技术应用开发、Ajax框架与实例专题,或进入讨论组讨论。
相关专题
- Ajax技术专题 (558篇文章)
- Ajax技术应用开发 (558篇文章)
- Ajax框架与实例 (221篇文章)
- Ajax技术资讯 (81篇文章)
- Ajax基础教程 (96篇文章)
- Ajax的WEB开发 (141篇文章)
- Ajax技术开发 (138篇文章)
- 开发ASP.NET AJAX客户端定制行为 (0次浏览)
- 解决ajax获取页面直接显示乱码的方法 (0次浏览)
- 基于ASP.NET AJAX框架实现表单验证编程原理 (0次浏览)
- 基于ASP.NET AJAX框架实现表单验证编程(实践 (0次浏览)
- 基于ASP.NET AJAX框架实现表单验证编程(理论 (0次浏览)
- 从Atlas到Microsoft ASP.NET AJAX(3) (0次浏览)
- 从Atlas到Microsoft ASP.NET AJAX(2) (0次浏览)
- 从Atlas到Microsoft ASP.NET AJAX(1) (0次浏览)
- Codd 提出的 RDBMS 12项准则 (0次浏览)
- 不用组件实现Ajax效果 (0次浏览)



