爲了實現股票的市值即時刷新,需要編寫一段 AJAX程序。先將程序列出,然後再說明:
這個頁面判斷股票代碼爲上證,還是深證:SERVER2.ASP
<%
'判斷獲得的股票代碼標誌位
dim code,Ncode,StockName
Ncode=request("gpdm")
session("Ncode")=request("gpdm")
if Ncode="" then Ncode="600000"
if left(Ncode,3)="399" then response.Redirect("hq1.asp?code="&Ncode)
if left(Ncode,1)="0" or left(Ncode,1)=1 then
code="21"&Ncode
else
code="11"&Ncode
end if
Select Case left(Ncode,3)
Case "000"
code="21"&Ncode
Case "001"
code="21"&Ncode
Case "002"
code="21"&Ncode
Case "003"
code="21"&Ncode
Case "030"
code="21"&Ncode
Case "038"
code="21"&Ncode
Case "009"
code="11"&Ncode
Case "010"
code="11"&Ncode
Case "090"
code="11"&Ncode
Case "099"
code="11"&Ncode
Case "110"
code="11"&Ncode
Case "111"
code="21"&Ncode
Case "119"
code="21"&Ncode
Case "120"
code="11"&Ncode
Case "125"
code="21"&Ncode
Case "126"
code="21"&Ncode
Case "129"
code="11"&Ncode
Case "131"
code="21"&Ncode
Case "159"
code="21"&Ncode
Case "160"
code="21"&Ncode
Case "161"
code="21"&Ncode
Case "162"
code="21"&Ncode
Case "163"
code="21"&Ncode
Case "164"
code="21"&Ncode
Case "165"
code="21"&Ncode
Case "181"
code="11"&Ncode
Case "184"
code="21"&Ncode
Case "190"
code="11"&Ncode
Case "200"
code="21"&Ncode
Case "201"
code="11"&Ncode
Case "202"
code="11"&Ncode
Case "203"
code="11"&Ncode
Case "204"
code="11"&Ncode
Case "360"
code="21"&Ncode
Case "363"
code="21"&Ncode
Case "369"
code="21"&Ncode
Case "395"
code="21"&Ncode
Case "500"
code="11"&Ncode
Case "510"
code="11"&Ncode
Case "519"
code="11"&Ncode
Case "522"
code="11"&Ncode
Case "523"
code="11"&Ncode
Case "580"
code="11"&Ncode
Case "582"
code="11"&Ncode
Case "600"
code="11"&Ncode
Case "601"
code="11"&Ncode
Case "609"
code="11"&Ncode
Case "738"
code="11"&Ncode
Case "799"
code="11"&Ncode
Case "900"
code="11"&Ncode
Case "939"
code="11"&Ncode
End Select
if left(Ncode,3)="100" then
if Ncode="100087" or Ncode="100117" or Ncode="100196" or Ncode="100236" or Ncode="100726" or Ncode="100795" then
code="11"&Ncode
else
code="21"&Ncode
end if
end if
![]()
dim flag '將標誌位信息賦予'session'
if left(code,2)=21 then
session("flag")=1
else
session("flag")=2
end if
response.Redirect("index.asp")
%>
這個是AJAX後面處理的頁面,寫得噁心點,將就着用:
另外,我想說的一點是,網上看了不少的帖子,說JAVASCRIPT是客戶端的東東,獲取SESSION只能在服務端執行。我深表同意。不過,同志們提供的用JS讀取SESSION的語句好像不大對,起碼在我的這個頁面來說是不可以的。 測試成功的讀取格式:var flag= Session("flag");
var code= Session("Ncode");
另外,在這個頁面中,RS(“”)的使用也有些出入。就不寫出我查找到的了,正確的:
var zd2=rs("s8").Value-rs("s3").Value;
<% @Language="JavaScript" %>
![]()
<%
![]()
//2007-03-30 16:28 pm design by qurongtao uesd ajax
![]()
![]()
![]()
var flag= Session("flag");
var code= Session("Ncode");
![]()
![]()
function 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;
}
![]()
![]()
if (flag==2)
![]()
...{
var sResult = new Array();
var oConn = OpenDB("show2003.mdb");
![]()
var sql = "select * from show2003 where s1= '"+code+" ' ";
var rs = oConn.Execute(sql);
![]()
var zd= (rs("s8").Value-rs("s3").Value)/ rs("s3").Value;
var zd2=rs("s8").Value-rs("s3").Value;
//var kk=FormatNumber(zd,3)
![]()
![]()
}
else if (flag==1)
![]()
...{
var sResult = new Array();
var oConn = OpenDB("sjshq.mdb");
![]()
var sql = "select * from sjshq where HQZQDM = '"+code+" ' ";
var rs = oConn.Execute(sql);
var zd=rs("HQJSD").Value / rs("HQZRSP").Value
}
![]()
while(!rs.EOF)
![]()
![]()
...{
![]()
![]()
if (flag==2)
![]()
...{
sResult[sResult.length] = rs("s32").Value + "###" + rs("s30").Value+ "###" + rs("s24").Value+ "###" + rs("s22").Value+ "###" + rs("s10").Value+ "###" +rs("s9").Value+ "###"+ rs("s16").Value+ "###"+ rs("s18").Value+ "###"+ rs("s26").Value+ "###"+ rs("s28").Value+ "###"+ rs("S8").Value+ "###"+ rs("S3").Value+ "###"+ rs("S4").Value+"###"+zd2+ "###"+ rs("S7").Value+ "###"+ rs("S6").Value+"###"+zd+ "###"+ rs("S11").Value+ "###"+ rs("S5").Value;
}
else if (flag==1)
![]()
...{
sResult[sResult.length] = rs("HQSJW5").Value + "###" + rs("HQSJW4").Value+ "###" + rs("HQSJW3").Value+ "###" + rs("HQSJW2").Value+ "###" + rs("HQSJW1").Value+"###" + rs("HQBJW1").Value+ "###"+ rs("HQBJW2").Value+ "###"+ rs("HQBJW3").Value+ "###"+ rs("HQBJW4").Value+ "###"+ rs("HQBJW5").Value+ "###"+ rs("HQZJCJ").Value+ "###"+ rs("HQZRSP").Value+ "###"+ rs("HQJRKP").Value+ "###"+ rs("HQJSD1").Value+ "###"+ rs("HQZDCJ").Value+ "###"+ rs("HQZGCJ").Value+ "###"+zd+"###"+ rs("HQCJSL").Value+ "###"+ rs("HQCJJE").Value;
}
![]()
rs.MoveNext();
}
![]()
![]()
Response.Write(escape(sResult.join("###")));
![]()
%>
最後是前臺顯示:
沒有做CSS定義,版面串版。好使就行了~~
<script language="JavaScript">
function GetResult()
![]()
...{
![]()
var oBao = new ActiveXObject("Microsoft.XMLHTTP");
oBao.open("POST","Server.asp",false);
oBao.send();
var strResult = unescape(oBao.responseText);
//將字符串分開.
var arrResult = strResult.split("###");
RemoveRow(); //刪除以前的數據.
//將取得的字符串分開,並寫入表格中.
for(var i=0;i<arrResult.length;i++)
![]()
...{
arrTmp = arrResult[i].split("@@@");
s4 = arrTmp[0]; //字段num1的值
//s6 = arrTmp[1]; //字段num2的值
//s7 = arrTmp[2]; //字段num3的值
row1 = tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = s4;
//cell2 = row1.insertCell();
//cell2.innerText = s6;
//cell3 = row1.insertCell();
//cell3.innerText = s7;
}
}
![]()
function RemoveRow()
![]()
...{
//保留第一行表頭,其餘數據均刪除.
var iRows = tb.rows.length;
for(var i=0;i<iRows-1;i++)
![]()
...{
tb.deleteRow(1);
}
}
![]()
function MyShow()
![]()
...{
//2秒自動刷新一次,2秒取得一次數據.
timer = window.setInterval("GetResult()",2000);
}
</script>
![]()
<style type="text/css">
<!--
![]()
.pdLeft5 ...{ PADDING-LEFT: 5px; PADDING-TOP: 2px; HEIGHT: 20px
}
![]()
.curL ...{ PADDING-LEFT: 5px; FONT-WEIGHT: bold; COLOR: #ffffff; PADDING-TOP: 2px; HEIGHT: 21px; BACKGROUND-COLOR: #007cc8
}
![]()
.pdLeft5b ...{ PADDING-LEFT: 5px; PADDING-TOP: 2px
}
![]()
.STYLE3 ...{
font-family: "宋體";
font-size: 10;
}
![]()
.STYLE4 ...{
PADDING-LEFT: 5px;
PADDING-TOP: 2px;
HEIGHT: 20px;
font-family: "宋體";
font-size: 10;
}
![]()
.STYLE5 ...{font-size: 10px}
-->
</style>
<body onLoad="MyShow()">
<table width="820" border="1" align="left">
<tr>
<th width="24" class="STYLE3" scope="row"> </th>
<td width="780" rowspan="20" align="left" valign="top" class="STYLE3"><table width="200" border="1" id="tb">
<tr>
<th scope="row"><span class="STYLE5"></span></th>
</tr>
</table></td>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5">賣 五(元/手)</span></th>
</tr>
<tr>
<TD class=STYLE4>賣 四(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>賣 三(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>賣 二(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>賣 一(元/手)</TD>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="curL">當前價(元)</span></th>
</tr>
<tr>
<TD class=STYLE4>買 一(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>買 二(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>買 三(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>買 四(元/手)</TD>
</tr>
<tr>
<TD class=STYLE4>買 五(元/手)</TD>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">昨收盤</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">今開盤</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">漲跌額</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">最低價</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">最高價</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">漲跌幅</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">成交量(手)</span></th>
</tr>
<tr>
<th class="STYLE3" scope="row"><span class="pdLeft5b">成交額(萬元)</span></th>
</tr>
</table>
<pre class="STYLE3"> </pre>
![]()
最後的總結,JAVASCRIPT 不叫不熟悉,所以有些代碼直接用VBS寫了。其實我覺得JS+VBS也不錯的。JS 實現功能,VBS處理數據。
待補充中…… 業餘專業選手