xml| Data | refresh | no refresh
Traditionally, we browsed the Web to add the latest data. It can only be displayed when we re requesting the server side. However, for some time-sensitive websites, the traditional approach is not enough.
We can get the program to automatically refresh and periodically request data from the server. 5 Seconds to take the data, 10 seconds to take the data. Use XMLHTTP to issue requests and obtain data. Uploaded to the client, the client organizes and displays the data.
Demo.htm foreground display.
<script language= "JavaScript" >function GetResult () {/**---------------getresult ()-----------------* GetResult () * Function: Send the request via XMLHTTP, return the result. * Parameter: str, string, send condition. * Instance: GetResult (); *---------------GetResult ()---------------- -*/var Obao = new ActiveXObject ("Microsoft.XMLHTTP");//special characters: +,%,&,=, etc transmission solution. The string is encoded first with escape.//update:2004-6-1 12:22obao.open ("POST", "server.asp", false); Obao.send ();//server-side processing returns an escape-encoded string. var strresult = unescape ( Obao.responsetext);//Separates strings. var Arrresult = strresult.split ("###"); Removerow (); Deletes the previous data.//separates the obtained strings and writes to the table. for (Var i=0;i<arrresult.length;i++) {arrtmp = Arrresult[i].split ("@@@"); NUM1 = ARRTMP[0]; The value of the field num1 num2 = arrtmp[1]; The value of the field num2 = Row1 = Tb.insertrow (); cell1 = Row1.insertcell (); cell1.innertext = Num1;cell2 = Row1.insertcell (); Cell2.innertext = num2;} function Removerow () {//retains the first row header and all remaining data is deleted. var irows = Tb.rows.length;for (var i=0;i<irows-1;i++) {Tb.deleterow (1);}} function Myshow () {//2 seconds automatically refresh once, 2 seconds to obtain data. Timer = Window.setinterval ("GetResult ()", 2000);} </script><body ><p></p><table width= "47%" height= "border=" "0" cellpadding= "1" cellspacing= "0" id= "TB" ><tr><td>num1</td><td>num2</td></tr></table >
server.asp Background Read Data
<% @Language = "JavaScript"%><%function opendb (sdbname) {/**---------------opendb (sdbname)---------------- -* opendb (sdbname) * Function: Open database Sdbname, return conn object. * Parameters: Sdbname, String, database name. * instance: var conn = opendb ("Database.mdb"); *--------- ------OPENDB (sdbname)-----------------*/var connstr = "provider=microsoft.jet.oledb.4.0; Data source= "+server.mappath (Sdbname); var conn = Server.CreateObject (" ADODB. Connection "); Conn. Open (CONNSTR); return conn} var sresult = new Array (); var oconn = opendb ("Data.mdb");//special characters: +,%,&,=,? etc Transmission solutions. The client character is an escape encoded// So the server-side first has to go through unescape decoding.//update:2004-6-1 12:22var sql = "Select num1,num2 from Nums order by id"; var rs = oconn.execute (SQL) ; while (!rs. EOF) {//A record is separated by "###". Each column of data is separated by a "@@@". This is in the case of only two columns of data. Sresult[sresult.length] = rs ("Num1"). Value + "@@@" + RS ("num2"). Valuers.movenext ();} Escape solved the XMLHTTP. Chinese to deal with the problem. Response.Write (Escape (Sresult.join ("###"));%>
Database Data.mdb
Table Nums
ID, automatic numbering
NUM1, text
num2, text
Test data
ID NUM1 num2
1 20.70 20.810
2 10.5 20.5
3 12.3 300
4 132 323
5 563 56
6 20 10