This is a modified version of an example you can find at w3schools.com.
<script type="text/javascript">
function loadXMLDoc(theURL)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", theURL, false);
xmlhttp.send();
}
</script>
So just make "example.html" be any sort of path (relative or absolute) to the page you want to load, and xmlhttp.responseText
will be a string containing the response content. You can also use xmlhttp.responseXML
if you want it to be stored as a traversable XML document. Anyway, just assign either of those to a variable of your choice, and you will have it!
Note that 'loadXMLDoc' does not return anything directly but defines one of its members ('onreadystatechange') to do that job, and to does it only in certain condition (readyState and status). Conclusion - do not assign the output of this function to any var. Rather do something like:
var xmlhttp=false;
loadXMLDoc('http://myhost/mycontent.htmlpart');
if(xmlhttp==false){ /* set timeout or alert() */ }
else { /* assign `xmlhttp.responseText` to some var */ }
Without that, all one can see is 'undefined'...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…