Example of a pop-up box in the lower right corner using AJAX

Source: Internet
Author: User
Keywords: Example dialog box

Java code for JS File Content
  1. // Define the XMLHttp instance
  2. Var xmlHttp;
  3. Function createXMLHttpRequest (){
  4. // Start initializing the XMLHttpRequest object
  5. If (window. ActiveXObject ){
  6. // IE browser
  7. XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
  8. } Else if (window. XMLHttpRequest ){
  9. // Mozilla Browser
  10. XmlHttp = new XMLHttpRequest ();
  11. }
  12. If (xmlHttp ){
  13. // Set the request address
  14. XmlHttp. open ("GET", "message. do? Cmd = get ", true );
  15. XmlHttp. onreadystatechange = setState; // sets the callback function.
  16. XmlHttp. send (null );
  17. }
  18. }
  19. // Callback function
  20. Function setState (){
  21. If (xmlHttp. readyState = 4 ){
  22. If (xmlHttp. status = 200 ){
  23. ParseResults (xmlHttp. responseXML)
  24. }
  25. }
  26. }
  27. /* Process XML */
  28. Function parseResults (xmlDOM ){
  29. Try {
  30. Var root = xmlDOM.doc umentElement;
  31. Var info = root. getElementsByTagName ('message ');
  32. Var out = "";
  33. Var message = null;
  34. Var current = null;
  35. For (var I = 0; I <info. length; I ++ ){
  36. Message = info [I];
  37. Id = message. getElementsByTagName ("id") [0]. firstChild. data;
  38. Title = message. getElementsByTagName ("title") [0]. firstChild. data;
  39. Out = out + (I + 1) + ". "+" <a style = \ "font-size: 12px; color: #6D93C8; face: Tahoma \" title = 'content feed: \ n"
  40. + Title
  41. + "\ N time :"
  42. + Title
  43. + "'" + "Href = #>"
  44. + Title
  45. + "</A> <BR> ";
  46. }
  47. Popmsg ("<BR>" + out );
  48. } Catch (exception ){
  49. // Alert ("error! ");
  50. }
  51. }
  52. /* Prompt box in the lower right corner */
  53. Var oPopup = window. createPopup ();
  54. Var popTop = 50;
  55. Function popshow (){
  56. OPopup. show (screen. width-250, screen. height-popTop, 241,172 );
  57. }
  58. Function pophide (){
  59. OPopup. hide ();
  60. }
  61. Function popmsg (msgstr ){
  62. Var winstr = "<table valign = \" top \ "style = \" border: 1 solid #6D93C8 \ "width = \" 241 \ "height = \" 172 \ "border = \" 0 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "> ";
  63. Winstr + = "<tr> <td bgcolor = \" # BCCCDF \ "onClick = \" parent. pophide (); \ "valign = \" top \ "align = \" center \ "height = \" 20 \ "style = \" font-size: 14px; color: #6D93C8; face: Tahoma; filter: Blur (add = 1, direction = 45, strength = 8) \ "> you have a new short message: </td> </tr> <td valign = \ "top \" align = \ "center \"> <table valign = \ "top \" width = \ "90% \" height = \ "110 \" border = \ "0 \" cellpadding = \ "0 \" cellspacing = \ "0 \"> ";
  64. Winstr + = "<tr> <td valign = \" top \ "style = \" font-size: 12px; color: #6D93C8; face: tahoma \ ">" + msgstr + "</td> </tr> </table> ";
  65. OPopup.doc ument. body. innerHTML = winstr;
  66. Popshow ();
  67. SetInterval ('window. focus () ', 0); // re-obtain the focus of IE
  68. }
  69. /* Reminder interval */
  70. Var secs = 5; // seconds for the first prompt
  71. Function checkServer (secs ){
  72. For (I = 1; I <= secs; I ++ ){
  73. Window. setTimeout ("update (" + I + ")", I * 1000 );
  74. }
  75. }
  76. Function update (num ){
  77. If (num = secs ){
  78. CreateXMLHttpRequest ();
  79. Secs = 1*60; // seconds
  80. For (I = 1; I <= secs; I ++ ){
  81. Window. setTimeout ("update (" + I + ")", I * 1000 );
  82. }
  83. }
  84. Else {
  85. Printnr = secs-num;
  86. }
  87. }
  88. CheckServer (secs );
// Define the XMLHttp instance var xmlHttp; function createXMLHttpRequest () {// start initializing the XMLHttpRequest object if (window. activeXObject) {// xml http = new ActiveXObject ("Microsoft. XMLHTTP ");} else if (window. XMLHttpRequest) {// Mozilla xmlHttp = new XMLHttpRequest ();} if (xmlHttp) {// set the request address xmlHttp. open ("GET", "message. do? Cmd = get ", true); xmlHttp. onreadystatechange = setState; // sets the callback function xmlHttp. send (null) ;}// callback function setState () {if (xmlHttp. readyState = 4) {if (xmlHttp. status = 200) {parseResults (xmlHttp. responseXML) }}/ * process XML */function parseResults (xmlDOM) {try {var root = xmlDOM.doc umentElement; var info = root. getElementsByTagName ('message'); var out = ""; var message = null; var current = null; for (var I = 0; I <inf O. length; I ++) {message = info [I]; id = message. getElementsByTagName ("id") [0]. firstChild. data; title = message. getElementsByTagName ("title") [0]. firstChild. data; out = out + (I + 1) + ". "+" <a style = \ "font-size: 12px; color: #6D93C8; face: Tahoma \" title = 'Content Abstract: \ n "+ title +" \ n time: "+ title +" '"+" href = #> "+ title +" </a> <BR> ";} popmsg ("<BR>" + out);} catch (exception) {// alert ("error! ") ;}}/* Prompt box at the bottom right corner */var oPopup = window. createPopup (); var popTop = 50; function popshow () {oPopup. show (screen. width-250, screen. height-popTop, 241,172);} function pophide () {oPopup. hide ();} function popmsg (msgstr) {var winstr = "<table valign = \" top \ "style = \" border: 1 solid #6D93C8 \ "width = \" 241 \ "height = \" 172 \ "border = \" 0 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "> "; winstr + = "<tr> <td bgcolor = \" # BCCCDF \ "onClick = \" parent. pophide (); \ "valign = \" top \ "align = \" center \ "height = \" 20 \ "style = \" font-size: 14px; color: #6D93C8; face: Tahoma; filter: Blur (add = 1, direction = 45, strength = 8) \ "> you have a new short message: </td> </tr> <td valign = \ "top \" align = \ "center \"> <table valign = \ "top \" width = \ "90% \" height = \ "110 \" border = \ "0 \" cellpadding = \ "0 \" cellspacing = \ "0 \"> "; winstr + = "<tr> <td valign = \" top \ "style = \" font-size: 12px; color: #6D93C8; face: tahoma \ ">" + msgstr + "</td> </tr> </table>" Your opopup.doc ument. body. innerHTML = winstr; popshow (); setInterval ('window. focus () ', 0); // Let IE re-obtain the focus}/* prompt interval */var secs = 5; // the first time the system prompts the second function checkServer (secs) {for (I = 1; I <= secs; I ++) {window. setTimeout ("update (" + I + ")", I * 1000) ;}} function update (num) {if (num = secs) {createXMLHttpRequest (); secs = 1*60; // The prompt time seconds for (I = 1; I <= secs; I ++) {window. setTimeout ("update (" + I + ")", I * 1000) ;}} else {printnr = secs-num ;}} checkServer (secs );

Java code for the XML content of the request

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Messages>
  3. <Message>
  4. <Id> 001 </id>
  5. <Title> inform you of coming to the meeting today </title>
  6. </Message>
  7. </Messages>
<? Xml version = "1.0" encoding = "UTF-8"?> <Messages> <message> <id> 001 </id> <title> notify you to come to the meeting today </title> </message> </messages>

If XML is to be dynamically generated, use the following Java code:

  1. <% @ Page import = "java. util. List, com. sunflower. model. Message" contentType = "text/xml; charset = UTF-8" %>
  2. <%
  3. Response. setContentType ("text/xml ");
  4. Response. setHeader ("Cache-Control", "no-cache ");
  5. List list = (List) request. getAttribute ("messages ");
  6. Out. println ("<messages> ");
  7. If (list! = Null)
  8. For (int I = 0; I <list. size (); I ++ ){
  9. Message objM = (Message) list. get (I );
  10. Out. println ("<message> ");
  11. Out. println ("<id>" + objM. getId () + "</id> ");
  12. Out. println ("<title>" + objM. getTitle () + "</title> ");
  13. Out. println ("</message> ");
  14. }
  15. Out. println ("</messages> ");
  16. %>
<%@ page import="java.util.List,com.sunflower.model.Message" contentType="text/xml;charset=utf-8"%><%response.setContentType("text/xml");response.setHeader("Cache-Control", "no-cache");List list = (List)request.getAttribute("messages");out.println("<messages>");if(list!=null)for(int i=0;i<list.size();i++){Message objM = (Message)list.get(i);out.println("<message>");out.println("<id>"+objM.getId()+"</id>");out.println("<title>"+objM.getTitle()+"</title>");out.println("</message>");}out.println("</messages>"); %>

Java code for HTML file content

  1. <Html>
  2. <Head>
  3. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  4. <Title> A prompt is displayed in the lower right corner. </title>
  5. </Head>
  6. <Script type = "text/javascript" src = "ajax. js"> </script>
  7. <Body>
  8. <Input type = "button" onclick = "createXMLHttpRequest ()" value = "test request"/>
  9. <Div id = "contain"> </div>
  10. </Body>
  11. </Html>

Original post address: http://jinghua.javaeye.com/blog/114323

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.