怎样局部刷新网页

JavaScript011

怎样局部刷新网页,第1张

大体有两种方法:

一种方法是使用框架,每一个框架内都可以载入一个完整的网页,而在框架所在的网页内,则会体现出“局部刷新”的效果。

另一种方法是使用Ajax,异步载入需要的数据后,使用JS对HTML文档 的DOM结构进行更改,实现“局部刷新”的效果。

你可以在以下文章中了解具体知识:

参考资料:

【HTML框架】http://www.w3school.com.cn/html/html_frames.asp

【AJAX简介】http://www.w3school.com.cn/php/php_ajax_intro.asp

js不剧新页面只刷新数据的方式有很多。

1、可以通过iframe做局部刷新,这个比较简单。

2、可以通过ajax技术做动态刷新,这个很成熟,整个web2.0的技术基础就是ajax。

3、可以通过websocket长连接进行数据更新,这个由于是长连接,对服务端压力有一定要求。

希望对你有帮助。

jsp中的局部刷新是通过ajax实现的。

利用JSP+ajax来实现局部页面刷新的小例子:

处理ajax请求的jsp文件:ajax.jsp

<%@ page contentType="text/htmlcharset=gb2312" %>

<%

//设置输出信息的格式及字符集

response.setContentType("text/xmlcharset=UTF-8")

response.setHeader("Cache-Control","no-cache")

out.println("<response>")

for(int i=0i<2i++){

out.println("<name>"+(int)(Math.random()*10)+

"号传感器</name>")

out.println("<count>" +(int)(Math.random()*100)+ "</count>")

}

out.println("</response>")

out.close()

%>

发送ajax请求的jsp文件:zx.jsp

<head>

<META http-equiv=Content-Type content="text/htmlcharset=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 = "ajax.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=200bgColor=#f5efe7 border=0>

<TR>

<TD align=middle 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>