2014-12-05 99 views
0

这是代码。我对JavaScript非常陌生,每天都在学习更多。这段代码来自一本教科书的例子。谢谢你的回复。我想问的另一个问题是如何在无序列表中显示返回的文本?这是否会包含在html事物中,还是可以在JavaScript文件中完成?如何在新窗口/选项卡中打开文本和xml文件?

window.addEventListener("load",initAll,false); 
 
var xhr = false; 
 

 
function initAll() { 
 
\t document.getElementById("makeTextRequest").addEventListener("click",getNewFile,false); 
 
\t document.getElementById("makeXMLRequest").addEventListener("click",getNewFile,false); 
 
} 
 

 
function getNewFile(evt) { 
 
\t makeRequest(this.href); 
 
\t evt.preventDefault(); 
 
} 
 

 
function makeRequest(url) { 
 
\t if (window.XMLHttpRequest) { 
 
\t \t xhr = new XMLHttpRequest(); 
 
\t } 
 
\t else { 
 
\t \t if (window.ActiveXObject) { 
 
\t \t \t try { 
 
\t \t \t \t xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
 
\t \t \t } 
 
\t \t \t catch (e) { 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
\t if (xhr) { 
 
\t \t xhr.addEventListener("readystatechange",showContents,false); 
 
\t \t xhr.open("GET", url, true); 
 
\t \t xhr.send(null); 
 
\t } 
 
\t else { 
 
\t \t document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest"; 
 
\t } 
 
} 
 

 
function showContents() { 
 
\t if (xhr.readyState == 4) { 
 
\t \t if (xhr.status == 200) { 
 
\t \t \t if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) { 
 
\t \t \t \t var outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]); 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t var outMsg = xhr.responseText; 
 
\t \t \t } 
 
\t \t } 
 
\t \t else { 
 
\t \t \t var outMsg = "There was a problem with the request " + xhr.status; 
 
\t \t } 
 
\t \t document.getElementById("updateArea").innerHTML = outMsg; 
 
\t } 
 
\t 
 
\t function getText(inVal) { 
 
\t \t if (inVal.textContent) { 
 
\t \t \t return inVal.textContent; 
 
\t \t } 
 
\t \t return inVal.text; 
 
\t } 
 
}

回答

1

通过它的外观,你这是一个AJAX请求,并正在接受XML。

在这种情况下,我想:

  1. 打开了window.open()一个新的页面(返回一个新的Window对象)
  2. ,然后更改新页面的document.body.innerHTML到XML你有

如果你有这样举行的XML网页(也许您的服务器被请求具有一个),你可以做:

window.open("page.xml");

相关问题