Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 16th, 2007, 12:26 PM
Authorized User
 
Join Date: Dec 2004
Location: Chesterfield, MO, USA.
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
Reply With Quote
  #2 (permalink)  
Old July 16th, 2007, 11:39 PM
Authorized User
 
Join Date: Jun 2007
Location: Tehran, Tehran, Iran.
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
Reply With Quote
  #3 (permalink)  
Old July 17th, 2007, 10:23 AM
Authorized User
 
Join Date: Dec 2004
Location: Chesterfield, MO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that did the trick.
Reply With Quote
  #4 (permalink)  
Old May 30th, 2008, 01:43 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Very great!!!!!!!

Reply With Quote
  #5 (permalink)  
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.
Reply With Quote
  #6 (permalink)  
Old April 10th, 2009, 04:04 AM
joefawcett's Avatar
Wrox Author
Points: 9,712, Level: 42
Points: 9,712, Level: 42 Points: 9,712, Level: 42 Points: 9,712, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,066
Thanks: 1
Thanked 36 Times in 35 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/
Reply With Quote
  #7 (permalink)  
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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 07: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 05:35 PM
xml document androger XML 2 November 19th, 2003 04:52 PM
XML Document Problem Ben Horne XML 5 November 19th, 2003 12:49 PM



All times are GMT -4. The time now is 10:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.