Ajax to achieve page timing refresh without brushing the screen

Source: Internet
Author: User

The main page (page requiring refresh) requires the system time to be refreshed every minute

<body onload="makeRequest("getSystemTime.jsp")"> //创建httpRequest对象
<form name="dateForm">
<table>
<td colspan="2" align="center">
     <div id="sysTimeShow"></div> //显示平台系统时间
    </td>
</table></form>
</body>
getSystemTime.jsp   //获得系统的当前时间,也就是输出结果的页面
<%@ page language="java" %>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.Date"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="java.io.IOException"%>
<%
Date d = new Date();
Timestamp ts = new Timestamp(d.getTime());
String result = ts.toString().substring(0,16);
out.write(result,0,result.length());
%>

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 ("Your browser does not support the current operation, please use IE 5.0 version!");
return false;
}
The method that defines the page invocation init, not init ();
Http_request.onreadystatechange = init;
Http_request.open (' Get ', url, true);
Prohibit IE caching
Http_request.setrequestheader ("If-modified-since", "0");
Send data
Http_request.send (NULL);
Refresh page every 60 seconds
settimeout ("MakeRequest" (' +url+ ")", 60000);
}
function init () {
if (http_request.readystate = = 4) {
if (http_request.status = 0 | | http_request.status = = 200) {
var result = Http_request.responsetext;
if (result== "") {
result = "System time acquisition Failure";
}
document.getElementById ("Systimeshow"). Innerhtml= "Platform system time:" +result;
else {//http_request.status!= 200
Alert ("Request failed!");
}
}
}

Related Article

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.