Jump to content

Ajax - Loading a HTML file

Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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)

Edited by Shakazahn
Link to post
Share on other sites

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>

Edited by dsonesuk
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...