Ajaxthe combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I am having trouble getting the xml object out of an iFrame. I'm using an iFrame to upload a document, which does xmlhttprequest doesn't seem to support. I'm trying the following script, which works great in FireFox but not in IE.
I have tried creating an XML document but could not figure out how load the contents using the a script I found at http://www.quirksmode.org/dom/importxml.html but I could not figure out how to load the contents of my iFrame into the xmlDoc created using the script there.
What can I do to get IE to recognize that the source of the iFrame is an XML document?
Code:
<!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=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="/js/ajaxobject.js"></script>
<script type="text/javascript">
function showContent(i) {
var theXML = i.contentWindow.document;
var errors = new Array()
var count = 0;
xml_errors = theXML.getElementsByTagName("error");
//check if any errros were returned
if(xml_errors.length>0) {
//loop through those errors
for(i=0;i<xml_errors.length;i++) {
//and if there is any data to display
if(xml_errors[i].firstChild) {
//assign it to the errors array.
errors[i] = xml_errors[i].firstChild.nodeValue;
count++;
}
}
}
if(errors.length) {
alert(errors[0]);
}
else {
alert("You're golden");
}
}
</script>
</head>
<body>
<p>I Frame</p>
<iframe id="iFrame" width="400" height="500" src="xml.php" onload="showContent(this)"></iframe>
</body>
</html>
The 'document' object in IE is not an xml document. It's a DHTML document and has some useful methods and properties to manipulate HTML document dynamically. If you need to access loaded document as a DOM document, you can use 'XMLDocument' property of this object. Meaning, you should change your code to something like this :
var theXML = i.contentWindow.document.XMLDocument;
The 'document' object in IE is not an xml document. It's a DHTML document and has some useful methods and properties to manipulate HTML document dynamically. If you need to access loaded document as a DOM document, you can use 'XMLDocument' property of this object. Meaning, you should change your code to something like this :
var theXML = i.contentWindow.document.XMLDocument;
In IE7, the line above throws an "Access Denied" error.