@H_404_13@
@H_404_13@
一、创建一个WebService,在我的WebService中有这样(HasNewMessage,原型是public string HasNewMessage(string userID,out int mailCount,out int pendingCount))一个方法,用IE访问WebService调用这个方法会有下面的一串SOAP请求消息格式. @H_404_13@
@H_404_13@
@H_404_13@
Host: 192.168.7.108@H_404_13@
Content-Type: text/xml; charset=utf-8@H_404_13@
Content-Length: length@H_404_13@
SOAPAction: "http://tempuri.org/HasNewMessage"@H_404_13@
@H_404_13@
<?xml version="1.0" encoding="utf-8"?> @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@
我们就可以根据它来编写我们的SOAP消息了。WebService将会以下面的格式返回我们的请求。 @H_404_13@
@H_404_13@
@H_404_13@
Content-Type: text/xml; charset=utf-8@H_404_13@
Content-Length: length@H_404_13@
@H_404_13@
<?xml version="1.0" encoding="utf-8"?> @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@ @H_404_13@
在这个函数中,我有三个输出,其实mailCount,pendingCount是输出参数.在程序中我可以根据这个格式来分解出我们所获得的输出。 @H_404_13@
@H_404_13@
二、用javascript访问WebService @H_404_13@
@H_404_13@
function HasNewMail() @H_404_13@
@H_404_13@
{ @H_404_13@
@H_404_13@
var userID = "3011"; @H_404_13@
@H_404_13@
var myService = "http://192.168.7.108/WebService/NewsService.asmx" ; @H_404_13@
@H_404_13@
var myMethod = "http://tempuri.org/HasNewMessage"; @H_404_13@
@H_404_13@
var requestHttp = new ActiveXObject("Microsoft.XMLHTTP"); @H_404_13@
@H_404_13@
var requestBody = ""; @H_404_13@
@H_404_13@
requestBody = requestBody + "<?xml version=/"1.0/" encoding=/"utf-8/"?>/n" ; @H_404_13@
@H_404_13@
requestBody = requestBody + "<soap:Envelope "; @H_404_13@
@H_404_13@
requestBody = requestBody + "xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" "; @H_404_13@
@H_404_13@
requestBody = requestBody + "xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" " ; @H_404_13@
@H_404_13@
requestBody = requestBody + "xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">/n" ; @H_404_13@
@H_404_13@
requestBody = requestBody + " <soap:Body>/n" ; @H_404_13@
@H_404_13@
requestBody = requestBody + " <HasNewMessage xmlns=/"http://tempuri.org//">/n" ; @H_404_13@
@H_404_13@
requestBody = requestBody + " <userID>" + userID + "</userID>/n"; @H_404_13@
@H_404_13@
requestBody = requestBody + " </HasNewMessage>/n"; @H_404_13@
@H_404_13@
requestBody = requestBody + " </soap:Body>/n"; @H_404_13@
@H_404_13@
requestBody = requestBody + "</soap:Envelope>"; @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
requestHttp.Open("POST",myService,false); @H_404_13@
@H_404_13@
requestHttp.SetRequestHeader("Content-Type","text/xml;charset=gb2312"); @H_404_13@
@H_404_13@
requestHttp.SetRequestHeader("SOAPAction",myMethod); @H_404_13@
@H_404_13@
requestHttp.Send(requestBody); @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
var result = requestHttp.ResponseXML; @H_404_13@
@H_404_13@
var pos1 = result .xml.indexOf("<HasNewMessageResult>"); @H_404_13@
@H_404_13@
var pos2 = result .xml.indexOf("</HasNewMessageResult>"); @H_404_13@
@H_404_13@
var len = pos2 - pos1 - ("<HasNewMessageResult>").length; @H_404_13@
@H_404_13@
var userName = result.xml.substr(pos1 + ("<HasNewMessageResult>").length,len); @H_404_13@
@H_404_13@
pos1 = result.xml.indexOf("<mailCount>"); @H_404_13@
@H_404_13@
pos2 = result.xml.indexOf("</mailCount>"); @H_404_13@
@H_404_13@
len = pos2 - pos1 - ("<mailCount>").length; @H_404_13@
@H_404_13@
var mailCount = result.xml.substr(pos1 + ("<mailCount>").length,len); @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
pos1 = result.xml.indexOf("<pendingCount>"); @H_404_13@
@H_404_13@
pos2 = result.xml.indexOf("</pendingCount>"); @H_404_13@
@H_404_13@
len = pos2 - pos1 - ("<pendingCount>").length; @H_404_13@
@H_404_13@
var pendingCount= result.xml.substr(pos1 + ("<pendingCount>").length,len); @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
var allCount = parseInt(mailCount) + parseInt(pendingCount); @H_404_13@
@H_404_13@
@H_404_13@
if(allCount > 0) @H_404_13@
@H_404_13@
PopUpNotify(userName,allCount,mailCount,pendingCount) @H_404_13@
@H_404_13@
} @H_404_13@
@H_404_13@
这个函数首先创建一个ActiveXObj利用它来完成对WebService的访问。然后根据上面介绍的格式创建SOAP消息体,与WebService交互。最后对所得到的WebService返回消息进行分解,得出所需的数据。 @H_404_13@
@H_404_13@
三、Pop窗口的弹出。Pop窗口的弹出有几种方法,好像也有这方面的专门控件,由于用javascript来完成,选用层的移动应用或PopupObject来实现,它可以让显示信息自下而上移动,但在测试过程中发现直接用层会被框架遮住。所以选用PopupObject,但存在一个问题就是用户不能控制这个PopupObject的关闭,它的关闭方式是在父窗体获得焦点以后就会自动关闭,在层实体的”self.close()”,不能很好的工作,它会弹出确认关闭窗口,随后自己就关闭了。以下是弹出函数和层实体(复制别人用层写出的弹出消息的一个层实体,把它作为PopupObject的innerHTML也能很的显示,真好!)。 @H_404_13@
@H_404_13@
var oPopup = window.createPopup(); @H_404_13@
@H_404_13@
function PopUpNotify(userName,pendigCount) @H_404_13@
@H_404_13@
{ @H_404_13@
@H_404_13@
var oPopupBody = oPopup.document.body; @H_404_13@
@H_404_13@
var HTMLBody = eMeng.innerHTML; @H_404_13@
@H_404_13@
HTMLBody = HTMLBody.replace(/userName/g,userName); @H_404_13@
@H_404_13@
HTMLBody = HTMLBody.replace(/allCount/g,allCount); @H_404_13@
@H_404_13@
HTMLBody = HTMLBody.replace(/mailCount/g,mailCount); @H_404_13@
@H_404_13@
HTMLBody = HTMLBody.replace(/pendingCount/g,pendigCount); @H_404_13@
@H_404_13@
oPopupBody.innerHTML = HTMLBody; @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
oPopup.show(screen.availWidth,screen.Height - 145,180,116); @H_404_13@
@H_404_13@
var realHeight = oPopupBody.scrollHeight; @H_404_13@
@H_404_13@
oPopup.hide(); @H_404_13@
@H_404_13@
// Shows the actual popup object with correct height. @H_404_13@
@H_404_13@
oPopup.show(screen.availWidth,116); @H_404_13@
@H_404_13@
window.setTimeout("oPopup.hide()",5000); @H_404_13@
@H_404_13@
} @H_404_13@
@H_404_13@
层实体如下: @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
<DIV id="eMeng" style="BORDER-RIGHT: #455690 1px solid; BORDER-TOP: #a6b4cf 1px solid; Z-INDEX: -1000; LEFT: 0px; VISIBILITY: hidden; BORDER-LEFT: #a6b4cf 1px solid; WIDTH: 180px; BORDER-BottOM: #455690 1px solid; POSITION: absolute; TOP: 0px; HEIGHT: 116px; BACKGROUND-COLOR: #c9d
@H_404_13@
<TABLE style="BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid" cellSpacing="0" @H_404_13@
@H_404_13@
cellPadding="0" width="100%" bgColor="#cfdef4" border="0" ID="Table8"> @H_404_13@
@H_404_13@
<TBODY> @H_404_13@
@H_404_13@
<TR> @H_404_13@
@H_404_13@
<TD vAlign="middle" width="30" height="24"><IMG src="../Images/Poppms.gif"></TD> @H_404_13@
@H_404_13@
<TD style="FONT-WEIGHT: normal; FONT-SIZE:
@H_404_13@
vAlign=center width="100%">系统提示:</TD> @H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
@H_404_13@
<TD vAlign="middle" align="right" width="19"><IMG title="关闭" style="CURSOR: hand" onclick= "self.close()" hspace="3" src="../Images/PopClose.gif"></TD> @H_404_13@
@H_404_13@
</TR> @H_404_13@
@H_404_13@
<TR> @H_404_13@
@H_404_13@
<TD colSpan="3" height="90"> @H_404_13@
@H_404_13@
<DIV style="BORDER-RIGHT: #b
@H_404_13@
您共有<B><FONT color="red">allCount</FONT></B>条新消息</FONT><BR> @H_404_13@
@H_404_13@
其中:<BR> @H_404_13@
@H_404_13@
<a href="#" onclick="parent.window.parent.document.frames[´CenterFrame´].location.href=´/Affairs/mail/ReceiveBox.aspx?Refresh=1´"> @H_404_13@
@H_404_13@
新邮件有 <FONT color="red"><B>mailCount</B></FONT> 封。</a><BR> @H_404_13@
@H_404_13@
<a href="#" onclick="parent.window.parent.document.frames[´CenterFrame´].location.href = ´/Affairs/Pending/Pending.aspx´"> @H_404_13@
@H_404_13@
新事件有 <FONT color="red"><B>pendingCount </B></FONT>条.</a><BR> @H_404_13@
@H_404_13@
</DIV> @H_404_13@
@H_404_13@
</TD> @H_404_13@
@H_404_13@
</TR> @H_404_13@
@H_404_13@
</TBODY></TABLE> @H_404_13@
@H_404_13@
</DIV> @H_404_13@
@H_404_13@
这样就可以实现访问WebService和显示相关信息了。当然了,定时访问还要设置一个函数setInterval@H_404_13@
function VisitService()@H_404_13@
{@H_404_13@
HasNewMail();@H_404_13@
window.setInterval("HasNewMail()",60000); @H_404_13@
}@H_404_13@
在页面加载事件中运行这个函数就能实现定时访问了。@H_404_13@
直接用层来实现Popup效果也是一种不错的方法,这里给别人写的代码。上面的层实体也从这个例子中得到的。@H_404_13@
var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0;@H_404_13@
var divBody = "";@H_404_13@
function SaveDIVInnerHTML()@H_404_13@
{@H_404_13@
divBody = document.getElementById("eMeng").innerHTML;@H_404_13@
}@H_404_13@
function getMsg(allCount,pendingCount)@H_404_13@
{@H_404_13@
try{@H_404_13@
divTop = parseInt(document.getElementById("eMeng").style.top,10)@H_404_13@
divLeft = parseInt(document.getElementById("eMeng").style.left,10)@H_404_13@
divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)@H_404_13@
divWidth = parseInt(document.getElementById("eMeng").offsetWidth,10)@H_404_13@
docWidth = document.body.clientWidth;@H_404_13@
docHeight = document.body.clientHeight;@H_404_13@
document.getElementById("eMeng").style.top = parseInt(document.body.scrollTop,10) + docHeight + 10;// divHeight@H_404_13@
document.getElementById("eMeng").style.left = parseInt(document.body.scrollLeft,10) + docWidth - divWidth@H_404_13@
document.getElementById("eMeng").style.visibility="visible"; @H_404_13@
alert(divBody);@H_404_13@
document.getElementById("eMeng").innerHTML = document.getElementById("eMeng").innerHTML.replace(/allCount/g,allCount);@H_404_13@
document.getElementById("eMeng").innerHTML = document.getElementById("eMeng").innerHTML.replace(/mailCount/g,mailCount);@H_404_13@
document.getElementById("eMeng").innerHTML = document.getElementById("eMeng").innerHTML.replace(/pendingCount/g,pendingCount);@H_404_13@
objTimer = window.setInterval("moveDiv()",10) @H_404_13@
}@H_404_13@
catch(e){}@H_404_13@
}@H_404_13@
function moveDiv()@H_404_13@
{@H_404_13@
try@H_404_13@
{@H_404_13@
if(parseInt(document.getElementById("eMeng").style.top,10) <= (docHeight - divHeight + parseInt(document.body.scrollTop,10)))@H_404_13@
{@H_404_13@
window.clearInterval(objTimer)@H_404_13@
objTimer = window.setInterval("resizeDiv()",1)@H_404_13@
}@H_404_13@
divTop = parseInt(document.getElementById("eMeng").style.top,10)@H_404_13@
document.getElementById("eMeng").style.top = divTop - 1@H_404_13@
}@H_404_13@
catch(e){}@H_404_13@
}@H_404_13@
function resizeDiv()@H_404_13@
{@H_404_13@
i+=1@H_404_13@
if(i>500) closeDiv()@H_404_13@
try{@H_404_13@
divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)@H_404_13@
docWidth = document.body.clientWidth;@H_404_13@
docHeight = document.body.clientHeight;@H_404_13@
document.getElementById("eMeng").style.top = docHeight - divHeight + parseInt(document.body.scrollTop,10)@H_404_13@
document.getElementById("eMeng").style.left = docWidth - divWidth + parseInt(document.body.scrollLeft,10)@H_404_13@
}@H_404_13@
catch(e){}@H_404_13@
}@H_404_13@
function closeDiv()@H_404_13@
{@H_404_13@
document.getElementById(´eMeng´).style.visibility=´hidden´;@H_404_13@
if(objTimer) window.clearInterval(objTimer)@H_404_13@
document.getElementById("eMeng").innerHTML = divBody;@H_404_13@
}@H_404_13@
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。