相信大家也遇到过这样的情况,在B/S的系统中即时的发送短消息,一般的办法是定时的刷新页面,但如果你也像我一样厌倦了不停闪动的页面,不妨来试试javascript+WebService的解决方法。
//deptCode是部门编号,用来检测是否有未读信息
function MoniMsg(deptCode)
{
var deptCode = document.getElementById("DeptCode").value;
var myWebService=" http://122.124.6.70/khmis/websvr/KH_Msg.asmx ";
var myMethod=" http://tempuri.org/hasNewMsg ";
var requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
var s=""; s=s + "
\n"
s=s + "
1<soap:envelope "="" ""="" +="" 2001="" \"="" envelope="" http:="" s="s" schemas.xmlsoap.org="" soap="" www.w3.org="" xmlns:soap='\"' xmlns:xsd='\"' xmlns:xsi='\"' xmlschema-instance\="" xmlschema\="">\n "
2s=s + " <soap:body>\n";
3
4//------------写入输入参数-----------------------------------------------
5s = s + " <hasnewmsg \"="" http:="" tempuri.org="" xmlns='\"'>\n ";
6s = s + " <deptcode>"+ deptCode +"</deptcode>\n";
7s = s + " </hasnewmsg>\n";
8//-----------------------------------------------------------------------
9
10s=s + " </soap:body>\n"
11s=s + " </soap:envelope>
"
//这里是重点哟!
requestHttp.Open("POST",myWebService,false);
requestHttp.SetRequestheader("Content-Type","text/xml;Charset=gb2312");
requestHttp.SetRequestheader("SOAPAction", myMethod);
requestHttp.Send(s);
res = requestHttp.ResponseXML;
var pos1 = res.xml.indexOf("
1<hasnewmsgresult>");
2var pos2 = res.xml.indexOf("</hasnewmsgresult>
");
var len = pos2 - pos1 - ("
1<hasnewmsgresult>").length;
2
3var result = res.xml.substr(pos1 + ("<hasnewmsgresult>").length , len);
4
5if (document.getElementById("IsHasHistoryMsg").value == "no")
6{
7
8}
9else
10{
11if (result == "true")
12{
13document.getElementById("msg").innerHTML ="<a href="javascript:ShowNewMsg(deptCode)" style="color:blue;text-decoration:none"> <img alt="点击查看" border="0" id="img1" name="img1" src="../Images/mail.gif" style="POSITION: relative; TOP: 5px"/></a>";
14}
15else
16{
17document.getElementById("msg").innerHTML ="<a href="javascript:ShowAllMsg(deptCode)" style="color:blue;text-decoration:none"><img alt="点击查看历史留言" border="0" src="../Images/mail2.gif" style="LEFT: 10px; POSITION: relative; TOP:6px"/></a>";
18}
19}
20}
21
22//WebService的代码很简单,就是查询数据库返回一个bool值,就不在这里写了,相信大家都能明白。函数在页面里这样使用window.setTimeout("MoniMsg("+deptCode+")",5000);,说完了,要不要赶快试一下?</hasnewmsgresult></hasnewmsgresult>