厌倦了闪动的页面?来试试 javascript+WebService 吧!

类别:.NET开发 点击:0 评论:0 推荐:

相信大家也遇到过这样的情况,在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 + "<?xml version =\"1.0\" encoding=\"gb2312\"?>\n"
  s=s + "<soap:Envelope "
  s=s + "  xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\""
  s=s + "  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
  s=s + "  xmlns:soap=\"\n'>http://schemas.xmlsoap.org/soap/envelope/\">\n"
  s=s + "   <soap:Body>\n";
  
  //------------写入输入参数-----------------------------------------------
  s = s + "   <hasNewMsg xmlns=\"\n'>http://tempuri.org/\">\n";
  s = s + "    <deptCode>"+ deptCode +"</deptCode>\n";
  s = s + "    </hasNewMsg>\n";
  //-----------------------------------------------------------------------
  
  s=s + "   </soap:Body>\n"
  s=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("<hasNewMsgResult>");  
  var pos2 = res.xml.indexOf("</hasNewMsgResult>");
  
  var len = pos2 - pos1 - ("<hasNewMsgResult>").length;
  
  var result = res.xml.substr(pos1 + ("<hasNewMsgResult>").length , len);  

  if (document.getElementById("IsHasHistoryMsg").value == "no")
  {
   
  }
  else
  {
   if (result == "true")
   {
    document.getElementById("msg").innerHTML ="<A href='javascript:ShowNewMsg(deptCode)' style='color:blue;text-decoration:none' >&nbsp;&nbsp;<img src='../Images/mail.gif' border=0 alt='点击查看' style='POSITION: relative; TOP: 5px'   id='img1' name='img1'></a>";
   }
   else
   {
         document.getElementById("msg").innerHTML ="<A href='javascript:ShowAllMsg(deptCode)' style='color:blue;text-decoration:none'><img src='../Images/mail2.gif' border=0 alt='点击查看历史留言' style='LEFT: 10px; POSITION: relative; TOP:6px'></a>";
     }
  }   
 }
 
//WebService的代码很简单,就是查询数据库返回一个bool值,就不在这里写了,相信大家都能明白。函数在页面里这样使用window.setTimeout("MoniMsg("+deptCode+")",5000);,说完了,要不要赶快试一下?

本文地址:http://com.8s8s.com/it/it41704.htm