JS调用WebService 无刷新获得在线消息

调用WebService传回的XML字符串:
"<Messages><Message><ID>1</ID><Title>今天心情不错!</Title></Message><Message><ID>2</ID><Title>明天又要上班了!</Title></Message></Messages>"

webservice.htc文件下载
====================================================================================
Top.aspx页面:

<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <script language="javascript" src="Message.js" type="text/javascript"></script>
<script language="javascript">
  
function SentOnlineMsg()
{
 var accepterID = document.getElementById('drpUserId').options[document.getElementById('drpUserId').selectedIndex].value;
 //alert(accepterID);

 //发送在线消息
 service.useService("../GetMess_WebService/GetMess.asmx?wsdl","GetMess");
 service.GetMess.callService(callback,"SentOnlineMsg",accepterID);

 setTimeout("SentOnlineMsg()",30000);
 //alert("定时");
}

function callback(res)
{
 //alert("callback");
 if (!res.error)
 {
  if(res.value!="" && res.value!=null)
  {
   
   //alert(res.value)
   
   var xmlDoc = new ActiveXObject("Microsoft.xmldom"); 
   xmlDoc.async = "false";
   xmlDoc.loadXML(res.value);
   var mtitles=xmlDoc.getElementsByTagName("Title");
   var mids=xmlDoc.getElementsByTagName("ID");
   var arraylength=mtitles.length;
   var contentArray = new Array();
   var linkArray = new Array();
   for(i=0;i<arraylength;i++)
   {
    contentArray[i] = mtitles(i).text;
    linkArray[i]='MessageDetail.aspx?ID='+mids(i).text;
   }
   popMessage(214,140,"消息提醒",contentArray,linkArray,10000);
  } 
 }
 else
 {
  alert("Error");
  //alert(res.errorDetail.string);
 }

}

function LoadMenu()
{
 //alert("Start");
 //启动在线消息发送
 SentOnlineMsg();  
}
</script>

</HEAD>
 <body onload="LoadMenu()">
  <form id="Form1" method="post" runat="server">
   
   <DIV id="service" style="BEHAVIOR: url(webservice.htc)"></div>
   
   <FONT face="宋体">用户切换</FONT>
    <asp:DropDownList id="drpUserId" runat="server"></asp:DropDownList>
  </form>
  </DIV>
 </body>
</HTML>

===================================================================================

Message.js:

/*
  消息弹出窗方法     
  
  参数说明:
  width   窗口宽度
  height  窗口高度
  title   窗口标题
  contentArray 消息内容数组
  linkArray  链接地址
  outTime  延时关闭时间(单位:豪秒)

 
  调用的例子:
  popMessage(214,140,'消息提醒',contentArray,linkArray,10000)

*/


var oPopup = window.createPopup();
var popTop=0;
var mytime;
var closeTime;
var flag=0;
function innerHTML(width,height,title,contentArray,linkArray)
{

 var TempArray = "";
 var i=0;
 var number=0;
 var zindex=9999;
 var html="";
 var width2=width-6;
 var height2=height-24;
 var PopupWinId="PopupWin";
 var PopupWinHeaderId="PopupWin_Header";
 var PopupWinTitleId="PopupWin_Title";
 var PopupWinContentId="PopupWin_Content";
 var PopupWinMessageId="PopupWin_Message";
 html=html+"<div id=\"service\" style=\"behavior:url(webservice.htc)\"></div>";
 html=html+"<div id=\""+PopupWinId+"\" style=\"background:#E0E9F8; border-right:1px solid #455690; border-bottom:1px solid #455690;border-left:1px solid #B9C9EF; border-top:1px solid #B9C9EF; position:absolute; z-index:"+zindex+";  width:"+width+"px; height:"+height+"px; left:0px; right:0px;\" onselectstart=\"return false;\" >\r\n"+
    "<div id=\""+PopupWinHeaderId+"\" style=\"cursor:default;position:absolute; left:2px; width:"+width2+"px; top:2px;height:14px;filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='#FFE0E9F8', EndColorStr='#FFFFFFFF');font:12px arial,sans-serif; color:#1F336B; text-decoration:none;\">\r\n"+
     "<span id=\""+PopupWinTitleId+"\">"+title+"</span>\r\n"+
     "<span style=\"position:absolute; right:0px; top:0px; cursor:pointer; color:#728EB8; font:bold 12px arial,sans-serif;position:absolute;\" onclick=\"parent.ClosePopupWin()\" onmousedown=\"event.cancelBubble=true;\" onmouseover=\"style.color='#455690';\" onmouseout=\"style.color='#728EB8';\">X</span>\r\n"+
    "</div>\r\n"+
    "<div id=\""+PopupWinContentId+"\" onmousedown=\"event.cancelBubble=true;\" style=\"border-left:1px solid #728EB8; border-top:1px solid #728EB8;border-bottom:1px solid #B9C9EF; border-right:1px solid #B9C9EF;background:#E0E9F8;padding:5px;pacing=4px; overflow:hidden; text-align:left; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='#FFE0E9F8', EndColorStr='#FFFFFFFF');position:absolute;OVERFLOW: auto; left:2px; width:"+width2+"px; top:20px; height:"+height2+"px;\">\r\n";
 for(i=0;i<contentArray.length;i++)
 {
  var tbName = contentArray[i].split("|");  
  number=number+1;
  html=html+"<span  style=\"cursor:hand; font:12px arial,sans-serif; color:#1F336B; text-decoration:none;\" id=\""+PopupWinMessageId+"\" onclick=\"parent.OpenWin('"+linkArray[i]+"','"+i+"')\">"+number+"."+tbName[0]+"</span><br>";
  
 }
 html=html+"</div>\r\n";
 html=html+"</div>\r\n";
 oPopup.document.body.innerHTML =html;
}

function popMessage(width,height,title,contentArray,linkArray,timeOut)
{
 flag=0;
 innerHTML(width,height,title,contentArray,linkArray);
 popupWin(width,height);
 window.setTimeout("CloseWin("+width+","+height+")",timeOut);
}

function ClosePopupWin()
{
 oPopup.show(0,0,0,0);
 flag=1;
 popTop=0;
 window.clearInterval(mytime);
 window.clearInterval(closeTime);
}

function CloseWin(width,height)
{
 if(flag==0)
 {
  window.clearInterval(mytime);
  if( height>0 && null!=oPopup)
  {
   oPopup.show(screen.width-width,screen.height-height-30, width, height);
  }
  else
  {
   window.clearInterval(closeTime);
   if(null!=oPopup)
   {
    oPopup.show(0,0,0,0);
    flag=1;
    popTop=0;
   }
   return;
  }
  height=height-0.8;
  closeTime=window.setTimeout("CloseWin("+width+","+height+");",5);
 }
}

function popupWin(width,height)
{

 if( popTop<=height)
 {
  oPopup.show(screen.width-width,screen.height-popTop, width, popTop);
 }
 
 else if(popTop<=height+30)
 {
  oPopup.show(screen.width-width,screen.height-popTop, width, height);
 }
 else
 {
  oPopup.show(screen.width-width,screen.height-height-30, width, height);
 }
 popTop=popTop+0.8;
 mytime=setTimeout("popupWin("+width+","+height+");",5);
}

 

 

 
 /*
 
 html=html+"<style type=\"text/css\"> "+
 html=html+"<!-- "+
 html=html+"body "+;
 html=html+"{ "+;
 html=html+"background-color:#FFFFFF; "+
 html=html+"scrollbar-face-color: #DEE3E7; "+
 html=html+"scrollbar-highlight-color: #FFFFFF; "+
 html=html+"scrollbar-shadow-color: #DEE3E7; "+
 html=html+"scrollbar-3dlight-color: #D1D7DC; "+
 html=html+"scrollbar-arrow-color:  #006699; "+
 html=html+"scrollbar-track-color: #EFEFEF; "+
 html=html+"scrollbar-darkshadow-color: #98AAB1; "+
 html=html+"} ";
 html=html+"--> ";
 html=html+"</style> "+
 
 */
 
 //html="<LINK href=\"../css/nandasoft.css\" type=\"text/css\" rel=\"stylesheet\">";

function OpenWin(strUrl,winName)
{
 var intHeight = 380;
 var intWidth = 600;
 var mytop=(screen.height- intHeight)/2;
 var myleft=(screen.width- intWidth)/2;
 var strType='resizable=no,scrollbars=no,width=' + intWidth + ',height=' + intHeight + ',left=' + myleft + ',top=' + mytop;
 var win=window.open(strUrl,winName,strType);
 win.focus();
}

posted @ 2007-01-15 21:57  RobotTech  阅读(1502)  评论(3编辑  收藏  举报