Shakazahn Posted May 28, 2010 Share Posted May 28, 2010 Hi,I'm trying to make a quick pre-defined layout builder for emails and such, and for that I'm using ajax.I'm storing some elements like:File Test.html <div> Testing...</div> And I'm using a Javascript code to load this file and display it on my main page. It works with .XML but I'd like it to work with .HTML files as well. function ajaxS(object, event) { xmlDoc = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function() { if (xmlDoc.readyState == 4) { if(xmlDoc.status == 200) { alert(xmlDoc.responseXML.childNodes[0].nodeName); } } }; xmlDoc.open("GET", "Test.html", true); xmlDoc.send(null);} I was pretty sure it used to work with HTML files but I'm getting a "xmlDoc is null" error when I call that function.Can anyone point me what am I doing wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 28, 2010 Share Posted May 28, 2010 The responseXML property is only populated if the server returned an XML content type. If the server returned an HTML content type the responseHTML property will contain the response. Link to comment Share on other sites More sharing options...
Shakazahn Posted May 28, 2010 Author Share Posted May 28, 2010 Weirdly enough, it's working fine now...I think the variable wasn't declared outside the function: var xmlDoc; And it was giving me null everytime I tried to use it inside the onreadystatechange function.Edit: Ah, I checked the responseHTML and I got the same error... and I'm getting the error again if I use responseXML as well, I guess it just worked because I changed that to responseText (which doesn't help me much since I need to loop through the objects inside the file) Link to comment Share on other sites More sharing options...
thescientist Posted May 28, 2010 Share Posted May 28, 2010 you should still declare it as var xmlDoc either way. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 29, 2010 Share Posted May 29, 2010 are you trying to achieve something like this? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script> <style type="text/css"></style></head><body><script type="text/javascript">/*<![CDATA[*//*---->*/function changeme() { try { xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { document.getElementById("myDiv").innerHTML =xmlhttp.responseText; } }; xmlhttp.open("GET", "Test.html"); xmlhttp.send(null); return false;}/*--*//*]]>*/</script> <a href="java script:void(0);" onclick="changeme();">Click me</a><div id="myDiv">change me</div></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.