Using AJAX technology to realize automatic local refresh of Web page without flash

Source: Internet
Author: User

In the process of Web page production, we often encounter the problem of timely refresh data, if the use of the method, will cause the entire screen flashing refresh effect, which will reduce the user's operational satisfaction.

Therefore, we need a way to achieve the automatic flash data-free method to solve the above problems.

instance to solve the problem:

Want to realize the user after entering the system (the entire session of the time limit), if you receive a new message will send a sound prompts.

Realize the idea:

1. Home part:< Body onload= "init ("); " Call Init (user) at >//load time;

2.js part: Using XMLHTTP to implement page local refresh, call check_mail.jsp to retrieve the background database and return the result.

<!--
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
var checkresult=null;
var username =null;

function init (user) {
Username=user;
Window.setinterval (' Checkmail () ', 5000);//every 5 seconds automatically call 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) {//judged as false
Test1.innerhtml= "&nbsp;";
Checkresult=1;
}
else{//judgment is true.
Test1.innerhtml= "<ccid_file alt= new mail values=" img/tp024 "
alt= New mail 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. Home results show

Insert < span id= "Test1" > </span > into the specified location.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.