Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
|
Ajax the 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2007, 12:26 PM
Authorized User
 
Join Date: Dec 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Get XML Document from iFrame

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>
And the XML src file is
Code:
<?PHP 
header("content-type:text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>' ;
?>

<stuff>
    <name>Robbert</name>
    <name>Kelly</name>
    <name>Jonathon</name>
    <name>Carl</name>
    <error>Uh Oh</error>
</stuff>
I changed
 
Old July 16th, 2007, 11:39 PM
Authorized User
 
Join Date: Jun 2007
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rvanandel

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;


Ehsan Zaery Moghaddam
 
Old July 17th, 2007, 10:23 AM
Authorized User
 
Join Date: Dec 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that did the trick.
 
Old May 30th, 2008, 01:43 AM
Registered User
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very great!!!!!!!

 
Old April 9th, 2009, 07:53 PM
Registered User
 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by zaerymoghaddam View Post
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.
 
Old April 10th, 2009, 04:04 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This can happen if the iframe's domain and the main window are not the same or even if they are being accessed via a different protocol.
__________________
Joe
http://joe.fawcett.name/
 
Old April 10th, 2009, 05:24 AM
Registered User
 
Join Date: Apr 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

Quote:
Originally Posted by joefawcett View Post
This can happen if the iframe's domain and the main window are not the same or even if they are being accessed via a different protocol.
Yah that's what was going on... fortunately I found another way to accomplish my end goal.





Similar Threads
Thread Thread Starter Forum Replies Last Post
loading XML Documents in IFrame in IE 5.0 anil_tiwari Javascript 1 February 2nd, 2006 08:01 PM
Document height within iframe... shenku Javascript 1 May 13th, 2005 02:39 PM
Help formatting an XML document!!! jrmsmo XML 2 February 13th, 2004 06:35 PM
xml document androger XML 2 November 19th, 2003 05:52 PM
XML Document Problem Ben Horne XML 5 November 19th, 2003 01:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.