0

I'm trying to pull an html variable from an iframe which is inside another div inside the iframe.

Here is the excerpt of the code I'm using:

var myVariable = $('#iframe-id').contents().find('.variable-inside').html();

The iframe has been give an id (#iframe-id) and the variable is inside "variable-inside" which is rendered inside the iframe.

For some reason this isn't working for me. Is there a problem with the code?

I'm aware this method can be used if both documents are on the same domain (iframe doc and other doc) but I can still use it if it's on the same domain, just different subdomains correct? Eg: iframe doc is at lorem.example.com and other doc is at ipsum.example.com)

Any help would be great. Thanks.

1 Answer 1

2

Communication across sub-domains is not allowed (Cross-Domain-Policy), I've read about ways to get around it but have not tested these.

Here's one method: http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/

The idea is to set document.domain = "yourdomain.com" on both the parent and child pages (untested).

Sign up to request clarification or add additional context in comments.

5 Comments

If push comes to shove, create a server-side proxy for the sub-domain-hosted-file. It would just grab the HTML from the external document and echo it out so you can access cross-domain data.
It worked! Just added <script type="text/javascript"> document.domain = 'example.com'; </script> to each page and it worked fine. Thank you.
it looks interesting, do you know if it works cross-domain ( not only sub-cross-domain ) ?
I don't have the time to test right now but if anyone has access to two domains it'd be pretty easy to test :)
@pawlakppp Just searched and found this: jszen.blogspot.com/2005/03/cross-domain-security-woes.html It appears it can work but has some issues with security and browsers. EDIT: Actually being 7 years old it is probably not the best source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.