使用原始Javascript加载XML

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用原始Javascript加载XML相关的知识,希望对你有一定的参考价值。

  1. /*
  2. Author: Alvin Crespo
  3. Description: XMLLoader
  4. */
  5.  
  6. //xml object variable
  7. var xmlhttp;
  8.  
  9. function loadXMLDoc(dname,type){
  10. //get the proper xml object (based on browser of user)
  11. xmlhttp = GetXMLHTTPObject();
  12.  
  13. //if no object was found, tell the user that their browser is not supported
  14. if (xmlhttp==null){
  15. alert ("Your browser is not supported!");
  16. return; //break the current function loadXMLDoc
  17. }
  18.  
  19. //keep track of the state
  20. xmlhttp.onreadystatechange = function(){
  21. //finishing state
  22. if (xmlhttp.readyState==4){
  23. //successful codes: 200 for webserver and 0 for local
  24. if (xmlhttp.status == 200 || xmlhttp.status == 0){
  25. //process code here
  26. if(type == "contact-cards")
  27. createContactCards(xmlhttp.responseXML); //located at contactcards.js
  28. if(type == "contact-profile")
  29. createProfile(xmlhttp.responseXML); //located at profiles.js
  30. }
  31. //let the user know that there was an error loading the xml document
  32. else{
  33. //should handle the error better
  34. alert("Error Loading XML");
  35. }
  36. }
  37. }
  38. //get the document and keep track of what is happening in a queue by setting asynch to true
  39. xmlhttp.open("GET",dname,true);
  40. xmlhttp.send("");
  41. }
  42.  
  43. //get the proper xml object
  44. function GetXMLHTTPObject(){
  45. if (window.XMLHttpRequest){ // for all browsers besides ie 5 and 6 and perhaps 7
  46. return new XMLHttpRequest();
  47. }else if (window.ActiveXObject){ // catch ie 5/6/7
  48. return new ActiveXObject("Microsoft.XMLHTTP");
  49. }
  50. return null;
  51. }
  52. //END OF XML LOADING

以上是关于使用原始Javascript加载XML的主要内容,如果未能解决你的问题,请参考以下文章

React 路由器在 url 地址栏重新加载时提供 javascript 对象而不是原始类型

使用JavaScript从JPEG访问原始YUV值

带有 Javascript 的 WebView 不断重新加载

在Javascript事件链中,浏览器的“返回原始位置”在刷新后会发生什么?

更改浏览器中的 URL 而不使用 JavaScript 加载新页面

Jquery实现滚动到底部加载更多(最原始)