views:

783

answers:

3

I have been having a lot of trouble figuring out how I can access the content within an iframe in Air. Here is some example jquery code that I have been testing with.


$(document).ready(function(){
    $("#frame").ready(function(){
     air.trace($("#frame").contents().find("body").html());
     air.trace(window.frames["frame"].innerHTML);
     air.trace(document.getElementById("frame").innerHTML);
    });
});

The iframe I am using is.

<iframe src="http://google.com" id="frame" width="100%" sandboxRoot="http://google.com/" documentRoot="/" name="frame" height="600"></iframe>

The output from the above code is


null
undefined


As you can see the contents are always either null, undefined or an empty string. Am I missing something that is preventing me from accessing the content of the iframe? Any suggestions would be greatly appreciated.

+1  A: 

Adobe AIR is just a browser (WebKit actually), and thus follows the rules browsers follow. The rule which governs your issue here is referred to as the "same-origin-policy".

If it's not on the same domain, the browser will not allow you to access the data or content from that site, so you will get this result. Use an Json based AJAX API if you would like to get data from another domain.

Example of someone else having this issue:

altCognito
i.e. the same origin policy
Rob
That does round it out a bit: added.
altCognito
I am familiar with the same origin policy but I didn't think that it would apply within air. I don't see many practical uses for a local iframe. This is actually disheartening as it will prevent me from adding the features that depended on being able to access the iframe's dom.
tomfmason
Actually, air is completely different. It has the ability to setup a sandbox bridge between the parent window and child iframe but you need access to the actual code code to expose anything from the child. This is a pita as all I want to do is read the dom. I don't care at all about calling methods within the frame. I guess my only alternative is to use a server side proxy and setup the bridge before returning the content to the client
tomfmason
@altCognito: AIR is definitely not just a repackaged browser. It includes a browser engine -- webkit, but not in its full glory.
dirkgently
I would be curious in a good way as to what the arguments are that it's not just a repackaged browser. There's nothing wrong with being a repackaged browser.
altCognito
+1  A: 

The solution was here http://help.adobe.com/en_US/AIR/1.1/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7f08.html

Here is an example of how I got it working.

<iframe src="http://google.com/local/iframe.html" id="frame" width="100%" sandboxRoot="http://google.com/local/" documentRoot="app:/" name="frame" height="600"></iframe>

Anything requested from the sandboxRoot(e.g. http://google.com/local) will be used from the document root and will be treated as if it is from the same domain(google.com).

In iframe.html I can access the iframe to google just as if it were from the same domain.

I knew there had to be a way to do this :)

tomfmason
I'm having the same problem and have some trouble understanding your solution. Could you explain a bit more? Especially why the src="http://google.com/local/iframe.html" when initially it was http://google.com??
trex279
Scratch that. I solved the problem. You are a livesaver.
trex279
A: 

hello,

im trying for few days now to access some elements in the iframe dom but i cant figure it out because here in adobe docs there is only shown how to access the parent from child

http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7eb2.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7edb