把url返回的数据,填充到指定id的div中
客户端: test.html
<script>
function createXMLHttp()
{
var ret = null;
try {
ret = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
ret = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (ee) {
ret = null;
}
}
if (!ret && typeof XMLHttpRequest != 'undefined')
ret = new XMLHttpRequest();
return ret;
}
function loadXmlToElement(fragment_url, element_id)
{
var element = document.getElementById(element_id);
element.innerHTML ='<img src="loading.gif" border="0"/>数据加载中...<br/>';
var xmlhttp = createXMLHttp();
xmlhttp.open("GET", fragment_url);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
//alert(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
element.innerHTML =
unescape(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
if(!element.innerHTML || element.innerHTML == "/n")
{
try{
element.innerHTML = unescape(xmlhttp.responseXML.firstChild.childNodes[1].nodeValue);
}catch(e){}
}
}
}
xmlhttp.send(null);
}
</script>
<div id="data">
</div>
<script>
var url="t.xml";
//alert(url);
loadXmlToElement(url,"data");
</script>
把url返回的数据,填充到指定id的div中
服务端:
test.xml
<?xml version='1.0' encoding='utf-8' ?>
<data>
<![CDATA[
this is a test
]]>
</data>
,注意服务器返回CDATA格式数据,内部可以是任何html代码