如题,是一个站内发送信件的程序我用js每隔8秒刷新一次,可成功获取通知收件信息
代码如下:var timeout = null; //setInterval函数句柄
var xmlHttp = false; //
//初始化XMLHttpRequest对象
function createXmlHttp(){
xmlHttp = false;
if (window.ActiveXObject) {
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
}else if (window.XMLHttpRequest) {xmlHttp = new XMLHttpRequest();}
}
//异步调用页面获取结果timestamp为了不让浏览器缓存结果
function sendRequest(){
createXmlHttp();
var url = "/message/getmessage.asp?timestamp=" + new Date().getTime();
if (!xmlHttp) {
alert("XMLHttpRequest is not Create!");
}
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function(){//回调函数开始
var tag = document.getElementById("newmsg");
tag.innerHTML = "";
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if (xmlHttp.responseText>0) {
tag.innerHTML= "<img src='/images/msg.gif' width='16' height='15' /> 您有[ "+xmlHttp.responseText+" ]封未读邮件";
}else{
tag.innerHTML="";
}
}
}//回调函数结束
xmlHttp.send(null);
}//开始自动刷新
function update(){
timeout = window.setInterval("sendRequest()", 10000);//设定10秒调用一次update页面
}
//停止自动刷新
function Stopupdate(){
if (timeout != null) {
window.clearInterval(timeout);
}
}以上代码能成功刷新并获取信息,显示到容器中。但是能否做到像qq那样的通知信息呢?也就是当获取到最新信息后,在网页的右下角弹出一个类似qq系统消息的对话框出来?
代码如下:var timeout = null; //setInterval函数句柄
var xmlHttp = false; //
//初始化XMLHttpRequest对象
function createXmlHttp(){
xmlHttp = false;
if (window.ActiveXObject) {
try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
}else if (window.XMLHttpRequest) {xmlHttp = new XMLHttpRequest();}
}
//异步调用页面获取结果timestamp为了不让浏览器缓存结果
function sendRequest(){
createXmlHttp();
var url = "/message/getmessage.asp?timestamp=" + new Date().getTime();
if (!xmlHttp) {
alert("XMLHttpRequest is not Create!");
}
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function(){//回调函数开始
var tag = document.getElementById("newmsg");
tag.innerHTML = "";
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if (xmlHttp.responseText>0) {
tag.innerHTML= "<img src='/images/msg.gif' width='16' height='15' /> 您有[ "+xmlHttp.responseText+" ]封未读邮件";
}else{
tag.innerHTML="";
}
}
}//回调函数结束
xmlHttp.send(null);
}//开始自动刷新
function update(){
timeout = window.setInterval("sendRequest()", 10000);//设定10秒调用一次update页面
}
//停止自动刷新
function Stopupdate(){
if (timeout != null) {
window.clearInterval(timeout);
}
}以上代码能成功刷新并获取信息,显示到容器中。但是能否做到像qq那样的通知信息呢?也就是当获取到最新信息后,在网页的右下角弹出一个类似qq系统消息的对话框出来?
<script language="JavaScript">
window.onload = getMsg;
window.onresize = resizeDiv;
window.onerror = function(){}
//短信提示使用(asilas添加)
var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0;
function getMsg()
{
try{
divTop = parseInt(document.getElementById("eMeng").style.top,10)
divLeft = parseInt(document.getElementById("eMeng").style.left,10)
divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)
divWidth = parseInt(document.getElementById("eMeng").offsetWidth,10)
docWidth = document.body.clientWidth;
docHeight = document.body.clientHeight;
document.getElementById("eMeng").style.top = parseInt(document.body.scrollTop,10) + docHeight + 10;// divHeight
document.getElementById("eMeng").style.left = parseInt(document.body.scrollLeft,10) + docWidth - divWidth
document.getElementById("eMeng").style.visibility="visible"
objTimer = window.setInterval("moveDiv()",10)
}
catch(e){}
}function resizeDiv()
{
i+=1
if(i>500) closeDiv()
try{
divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)
divWidth = parseInt(document.getElementById("eMeng").offsetWidth,10)
docWidth = document.body.clientWidth;
docHeight = document.body.clientHeight;
document.getElementById("eMeng").style.top = docHeight - divHeight + parseInt(document.body.scrollTop,10)
document.getElementById("eMeng").style.left = docWidth - divWidth + parseInt(document.body.scrollLeft,10)
}
catch(e){}
}function moveDiv()
{
try
{
if(parseInt(document.getElementById("eMeng").style.top,10) <= (docHeight - divHeight + parseInt(document.body.scrollTop,10)))
{
window.clearInterval(objTimer)
objTimer = window.setInterval("resizeDiv()",1)
}
divTop = parseInt(document.getElementById("eMeng").style.top,10)
document.getElementById("eMeng").style.top = divTop - 1
}
catch(e){}
}
function closeDiv()
{
document.getElementById('eMeng').style.visibility='hidden';
if(objTimer) window.clearInterval(objTimer)
}
</script>
<DIV id=eMeng style="BORDER-RIGHT: #455690 1px solid; BORDER-TOP: #a6b4cf 1px solid; Z-INDEX:99999; LEFT: 0px; VISIBILITY: hidden; BORDER-LEFT: #a6b4cf 1px solid; WIDTH: 180px; BORDER-BOTTOM: #455690 1px solid; POSITION: absolute; TOP: 0px; HEIGHT: 116px; BACKGROUND-COLOR: #c9d3f3">
<TABLE style="BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid" cellSpacing=0 cellPadding=0 width="100%" bgColor=#cfdef4 border=0>
<TBODY>
<TR>
<TD style="FONT-SIZE: 12px; BACKGROUND-IMAGE: url(msgTopBg.gif); COLOR: #0f2c8c" width=30 height=24></TD>
<TD style="FONT-WEIGHT: normal; FONT-SIZE: 12px; BACKGROUND-IMAGE: url(msgTopBg.gif); COLOR: #1f336b; PADDING-TOP: 4px;PADDING-left: 4px" vAlign=center width="100%"> 短消息提示:</TD>
<TD style="BACKGROUND-IMAGE: url(msgTopBg.gif); PADDING-TOP: 2px;PADDING-right:2px" vAlign=center align=right width=19><span title=关闭 style="CURSOR: hand;color:red;font-size:12px;font-weight:bold;margin-right:4px;" onclick=closeDiv() >×</span><!-- <IMG title=关闭 style="CURSOR: hand" onclick=closeDiv() hspace=3 src="msgClose.jpg"> --></TD>
</TR>
<TR>
<TD style="PADDING-RIGHT: 1px; BACKGROUND-IMAGE: url(1msgBottomBg.jpg); PADDING-BOTTOM: 1px" colSpan=3 height=90>
<DIV style="BORDER-RIGHT: #b9c9ef 1px solid; PADDING-RIGHT: 13px; BORDER-TOP: #728eb8 1px solid; PADDING-LEFT: 13px; FONT-SIZE: 12px; PADDING-BOTTOM: 13px; BORDER-LEFT: #728eb8 1px solid; WIDTH: 100%; COLOR: #1f336b; PADDING-TOP: 18px; BORDER-BOTTOM: #b9c9ef 1px solid; HEIGHT: 100%">您有<font color=#FF0000>1</font>封新短消息<BR><BR>
<DIV align=center style="word-break:break-all"><a href="javascript:alert('你好')"><font color=#FF0000>点击查看短信</font></a></DIV> </DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV>