boa下web页面动态刷新

来源:互联网 发布:电视为什么连不上网络 编辑:程序博客网 时间:2024/05/16 09:31

ajaxtest.html文件

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"/><title>Ajax + CGI Test</title><script language="JavaScript" src="ajaxtest.js"></script></head><body><h3>获取服务器当前温度</h3><p>服务器当前温度 是:<div id="current_time"></div></p><input type="button" value="获取" onclick="sender()"/></body></html>


ajaxtest.js文件

var xmlhttp;/**创建异步访问对象*/function createXHR(){        var xhr;        try        {                xhr = new ActiveXObject("Msxml2.XMLHTTP");        }        catch(e)        {                try                {                        xhr = new ActiveXObject("Microsoft.XMLHTTP");                }                catch(e2)                {                        xhr = false;                }        }        if(!xhr && typeof XMLHttpRequest !='undefined')        {                xhr = new XMLHttpRequest();        }        return xhr;}/**异步访问提交处理*/function sender(){        xmlhttp = createXHR();        if(xmlhttp)        {                xmlhttp.onreadystatechange=callbackFunction;                /*test.cgi后面跟个cur_time参数是为了防止Ajax页面缓存*/                xmlhttp.open("GET","ajaxtest.cgi?cur_time="+new Date().getTime(),true);                xmlhttp.send(null);        }        else        {                /*XMLHttpRequest对象创建失败*/                alert("浏览器不支持,请更换浏览器!");        }}/**异步回调函数处理*/function callbackFunction(){        if(xmlhttp.readyState == 4)        {                if(xmlhttp.status == 200)                {                        var returnValue = xmlhttp.responseText;                        if(returnValue != null && returnValue.length > 0)                        {                                document.getElementById("current_time").innerHTML = returnValue;                        }                        else                        {                                alert("结果为空!");                        }                }                else                {                        alert("页面出现异常!");                }        }}


 

服务器上应该有ajaxtest.cgi程序更新温度并返回温度

 以上html稍微修改下 放到定时器定时调用应该就可以

在sender()中,加上setTimeout,每隔两秒调用它本身就可以实现了。

 

感谢网名:armips的网友的贡献

另附参考文章:

http://wenku.baidu.com/view/98db2e68af1ffc4ffe47ace3.html

http://wenku.baidu.com/view/92bee9639b6648d7c1c74670.html

原创粉丝点击