为了实现股票的市值即时刷新,需要编写一段 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
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
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
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<%
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
2007-03-30 16:28 pm design by qurongtao uesd ajax
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
var
flag
=
Session(
"
flag
"
);
var
code
=
Session(
"
Ncode
"
);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function
OpenDB(sdbname)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
var connstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(sdbname);
var conn = Server.CreateObject("ADODB.Connection");
conn.Open(connstr);
return conn;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
if
(flag
==
2
)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
var sResult = new Array();
var oConn = OpenDB("show2003.mdb");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
var sql = "select * from show2003 where s1= '"+code+" ' ";
var rs = oConn.Execute(sql);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
var zd= (rs("s8").Value-rs("s3").Value)/ rs("s3").Value;
var zd2=rs("s8").Value-rs("s3").Value;
//var kk=FormatNumber(zd,3)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
else
if
(flag
==
1
)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
var sResult = new Array();
var oConn = OpenDB("sjshq.mdb");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
var sql = "select * from sjshq where HQZQDM = '"+code+" ' ";
var rs = oConn.Execute(sql);
var zd=rs("HQJSD").Value / rs("HQZRSP").Value
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
while
(
!
rs.EOF)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (flag==2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
rs.MoveNext();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Response.Write(escape(sResult.join(
"
###
"
)));
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
%>
最后是前台显示:
没有做CSS定义,版面串版。好使就行了~~
<
script language
=
"
JavaScript
"
>
function
GetResult()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function
RemoveRow()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//保留第一行表头,其余数据均删除.
var iRows = tb.rows.length;
for(var i=0;i<iRows-1;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tb.deleteRow(1);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function
MyShow()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//2秒自动刷新一次,2秒取得一次数据.
timer = window.setInterval("GetResult()",2000);
}
</
script
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
style type
=
"
text/css
"
>
<!--
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.pdLeft5
...
{ PADDING-LEFT: 5px; PADDING-TOP: 2px; HEIGHT: 20px
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.curL
...
{ PADDING-LEFT: 5px; FONT-WEIGHT: bold; COLOR: #ffffff; PADDING-TOP: 2px; HEIGHT: 21px; BACKGROUND-COLOR: #007cc8
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.pdLeft5b
...
{ PADDING-LEFT: 5px; PADDING-TOP: 2px
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.STYLE3
...
{
font-family: "宋体";
font-size: 10;
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.STYLE4
...
{
PADDING-LEFT: 5px;
PADDING-TOP: 2px;
HEIGHT: 20px;
font-family: "宋体";
font-size: 10;
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
.STYLE5
...
{font-size: 10px}
-->
</
style
>
<
body onLoad
=
"
MyShow()
"
>
<
table width
=
"
820
"
border
=
"
1
"
align
=
"
left
"
>
<
tr
>
<
th width
=
"
24
"
class
=
"
STYLE3
"
scope
=
"
row
"
>&
nbsp;
</
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
"
>&
nbsp;
</
pre
>
最后的总结,JAVASCRIPT 不叫不熟悉,所以有些代码直接用VBS写了。其实我觉得JS+VBS也不错的。JS 实现功能,VBS处理数据。
待补充中…… 业余专业选手