您的位置:首页 >> Web开发 >> XML技术 >> 正文
XML技术 RSS
 

xmlhttp技术的规范使用,标准xmlhttp数据岛技术的实现

http://www.rdxx.com 05年11月07日 02:59 phpe.net 我要投稿

关键词: 数据岛 , XMLHTTP , 技术 , 数据 , XML
xml数据岛功能网上文章不少,有的用vbs实现的,有的用不规范的js,导致非ie浏览器下无法正常使用。
为此,我参考了google网站的一些代码,并将其xml数据下载部分抽出,给于讲解,希望对其他人有所帮助。

建立三个文件如下:
Req.htm
Req.js
ReqXml.php

Req.htm代码如下:
CODE
  1. <html>
  2. <head><title>xml数据岛屿技术</title>
  3. <script language="javascript" src="Req.js"></script>
  4. <script language="javascript">
  5. function getAvailableNames() {
  6. var url = "ReqXml.php";
  7. //var url = "http://www.baidu.com";
  8. myxmlhttp = CreateXmlHttpReq(CheckAvailXmlHttpHandler);
  9. XmlHttpGET(myxmlhttp, url);
  10. }
  11. function CheckAvailXmlHttpHandler() {
  12. /* Ready when readystate == 4
  13. * Success when status == 200
  14. */
  15. if (myxmlhttp.readyState != 4 || myxmlhttp.status != 200) {
  16. return;
  17. }
  18. // get the values of the last inputs that were used to determine
  19. // usernames & save them.
  20. // extract the value of nextIndex from the response header
  21. var responseHeader = myxmlhttp.getAllResponseHeaders();
  22. // hidden variables to keep state information for the Handler
  23. // if the inputs (firstname, lastname, desired username) don't change, then
  24. // we want to update NextIndex so we show different username suggestions each time
  25. var nextIndex = parseResponseHeader("NextIndex", responseHeader);
  26. document.getElementById("errorDIV").innerHTML=myxmlhttp.responseText;
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <input type="button" name="checkavail" value="check availability!" style="font-size:9pt" onclick="getAvailableNames()">
  32. <div id="errorDIV"></div>
  33. </body>
  34. </html>



Req.js代码如下:
CODE
  1. //
  2. // Browser detect
  3. //
  4. var agt = navigator.userAgent.toLowerCase();
  5. var is_ie = (agt.indexOf("msie") != -1);
  6. var is_ie5 = (agt.indexOf("msie 5") != -1);
  7. var is_opera = (agt.indexOf("opera") != -1);
  8. var is_mac = (agt.indexOf("mac") != -1);
  9. var is_gecko = (agt.indexOf("gecko") != -1);
  10. var is_safari = (agt.indexOf("safari") != -1);
  11. function CreateXmlHttpReq(handler) {
  12. var xmlhttp = null;
  13. if (is_ie) {
  14. // Guaranteed to be ie5 or ie6
  15. var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
  16. try {
  17. xmlhttp = new ActiveXObject(control);
  18. xmlhttp.onreadystatechange = handler;
  19. } catch (ex) {
  20. // TODO: better help message
  21. alert("You need to enable active scripting and activeX controls");
  22. }
  23. } else {
  24. // Mozilla
  25. xmlhttp = new XMLHttpRequest();
  26. xmlhttp.onload = handler;
  27. xmlhttp.onerror = handler;
  28. }
  29. return xmlhttp;
  30. }
  31. function XmlHttpPOST(xmlhttp, url, data) {
  32. try {
  33. xmlhttp.open("POST", url, true);
  34. xmlhttp.send(data);
  35. } catch (ex) {
  36. // do nothing
  37. }
  38. }
  39. // XMLHttp send GEt request
  40. function XmlHttpGET(xmlhttp, url) {
  41. try {
  42. xmlhttp.open("GET", url, true);
  43. xmlhttp.send(null);
  44. } catch (ex) {
  45. // do nothing
  46. }
  47. }
  48. /**
  49. * parses the given xmlhttp response header for the given
  50. * key's value
  51. *
  52. * The header will look like this:
  53. * NextIndex: 4
  54. * PrevFirstName: John
  55. * PrevLastName: Smith
  56. * PrevDesired: John
  57. * Content-Type: text/html; charset=ISO-8859-1
  58. * Date: Mon, 03 Nov 2003 21:19:53 GMT
  59. * ...
  60. *
  61. */
  62. function parseResponseHeader(key, header) {
  63. var lines = header.split("\n");
  64. var re = new RegExp("^" + key + ":\\s");
  65. for (var i in lines) {
  66. if (re.exec(lines[i])) {
  67. var returnValue = trim(RegExp.rightContext);
  68. return returnValue;
  69. }
  70. }
  71. return "";
  72. }
  73. /**
  74. * Trim Function (trims leading and trailing whitespace)
  75. * This function is used by parseResponseHeader, needed because the
  76. * split function in IE doesn't strip the trailing "\n"
  77. */
  78. function trim(value) {
  79. var temp = value;
  80. var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
  81. if (obj.test(temp)) {
  82. temp = temp.replace(obj, '$2');
  83. }
  84. return temp;
  85. }


ReqXml.php代码如下:

共2页  1 2


 
 
标签: 数据岛 , XMLHTTP , 技术 , 数据 , XML 打印本文
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站