tags:

views:

32

answers:

3

I have an iFrame on my page which is displaying an XML document.

<iframe id="iFrame2" name="iFrame2"  width="100%" height="100%">

I need to access the XML document in the iFrame from the parent page. How do I do this?

When I try:

console.log(iFrame2.document);

I get the following error:

Permission denied for <http://localhost:3000&gt; to get property Window.document from <http://www.imageshack.us&gt;.
+1  A: 

See the comp.lang.javascript FAQ:

http://www.jibbering.com/faq/#frameRef

David Dorward
Thanks for your answer. I'm not sure if I'm reading correctly, but in order to get information from the iFrame, do I have to do a postMessage from the iFrame to the parent document? I don't have control over the XML doc, so if so, I'm not sure if I can do it.
ben
@ben: `postMessage` won't help you with XML; that's for working with HTML documents.
musicfreak
A: 

In javascript? Perhaps something like this document.getElementById('iFrame2').contentWindow.document

Andrew Kennan
+1  A: 

I don't believe there is a cross-browser way to access an XML document on a different domain. The easiest way around this is to set up a small proxy on your server that grabs the data, then simply make an Ajax call to the server.

I've never used Rails, so I don't know exactly what this would look like on the server-side (you'd have to create a separate question for that), but basically you would have it set up so that your server would, upon request, download that XML file and spit it back out as text. The client would then get this XML data using an Ajax request (Google that if you don't know what that is) and manipulate it with JavaScript.

So, assuming you are using jQuery, your domain is www.example.com, and you have a view called getxml that gets the XML data and returns it, you'd have something like this in the JavaScript:

$.get('http://www.example.com/getxml', function(xml) {
    // Manipulate the `xml` variable here
}, 'xml');

(See jQuery.get() for documentation.)

musicfreak
Thanks for your answer. I'm pretty new to web dev, could you possible point me in the direction of a tute on how to do this?
ben
@ben: Certainly. Are you doing any server-side development? Or are you only doing the JavaScript part of the development? If you are doing server-side, what language are you using?
musicfreak
@musicfreak: Thanks. I'm using Ruby on Rails.
ben
@ben: See my edit. All the functionality you need is provided by the jQuery library that I mentioned, although pretty much any JavaScript library can make Ajax requests.
musicfreak