javascript中ajax案例(copy)

來源:互聯網
上載者:User
(1)first.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"><html><head><title>first.html</title></head><body><p align="center">類比收件匣</p><hr><div align="center"
id="mailtable"></div><script
language="javascript">var xmlHttp=false;try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e2){xmlHttp=false;}}if(!xmlHttp && typeof
XMLHttpRequest !='undefined'){xmlHttp=new XMLHttpRequest();}//定義表格式資料var mail_list=new Array();function drawtable(){var table="<table border=\"1\" width=\"80%\"
id=\"table1\" cellspacing=\"0\"
cellpadding=\"0\">";table+="<tr><td
width=\"20%\">寄件者</td><td
width=\"60%\">標題</td><td
width=\"20%\">發件時間</td></tr>";for(i=0;i<mail_list.length;i++){table+="<tr><td
width=\"20%\">"+mail_list[i].sender+"</td><td
width=\"60%\">"+mail_list[i].title+"</td><td
width=\"20%\">"+mail_list[i].sendtime+"</td><tr>";}table+="</table>";window.document.getElementByIdx_x("mailtable").innerHTML=table;}//向表格中插入一條資料function addMail(id,sender,title,sendtime){var obj=new Object();obj.id=id;obj.sender=sender;obj.title=title;obj.sendtime=sendtime;for(i=mail_list.length;i>0;i--){mail_list[i]=mail_list[i-1];}mail_list[0]=obj;}//檢查伺服器是否有郵件到達checkmail();function checkmail(){var url="mailserver.jsp";xmlHttp.open("POST",url,false);xmlHttp.onreadystatechange=analresponse;//發送請求xmlHttp.send(null);setTimeout("checkmail()",5000);}function analresponse(){if(xmlHttp.readyState==4){var response=xmlHttp.responseText;if(response.length>1){//解析返回的資料,取出寄件者,標題,發件時間,更新頁面//獲得IDvar id=getstringfromdata(response,"id");var sender=getstringfromdata(response,"sender");var title=getstringfromdata(response,"title");var sendtime=getstringfromdata(response,"sendtime");if(id!=""){//在表格式資料中加入一條addMail(id,sender,title,sendtime);//重新畫表格drawtable();//如果當期瀏覽器不是焦點,則在工作列閃爍window.focus();}}}}function getstringfromdata(strin,tag){var tag1="<"+tag+">";var tag2="</"+tag+">";var i1=strin.indexOf(tag1);if(i1<0) return "";i1 += tag1.length;var i2=strin.indexOf(tag2);if(i2<0)return "";return strin.substring(i1,i2);}</script></body></html>(2)mailserver.jsp中<%@ page language="java" import="java.util.*"
pageEncoding="GB18030"%><%int x = Math.abs(new Random().nextInt(10));System.out.println("==========" + (new
Random().nextInt(10)));if (x % 10 == 3) {String mail = "<mail>" +
"<id>"+ Math.abs(new Random().nextInt()) +
"</id>"+ "<sender>寄件者" + x+ "</sender>" +
"<title>類比郵件標題"+ new Random().nextInt(1000) +
"</title>"+ "<sendtime>" + new
Date().toLocaleString()+ "</sendtime>" +
"</mail>";out.print(mail);System.out.println(mail);}%>即時提醒郵件功能;用ajax刷的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.