0

I am trying to something I really can't figure out. I have an iFrame loading some content from another domain on which I have no power. What I am "simply" trying to do is fetch the content of the iFrame to use it in PHP/Javascript. This is where I faced the "cross-domain" problem. I am unable to access the content of the iFrame. So frustrating.

So I read, and read some more and seen about the "postMessage()" function provided by HTML5. I saw a solution with this system, but I still can't figure one point. Basically, it works with a sender and a listener. So I need to have a listener in the iframe that, when triggered, will send the content back to the main window.

But HOW do I add some code in the already loaded iFrame without deleting the content ?

I don't really need to use postMessage(), I can be anything as long as I can get this damn content !

Any suggestion is appreciated !

Thank you !

Ben
  • 55
  • 5
  • Why not just skip the iFrame and use PHP's `file_get_contents()` function? – Aaron Dec 07 '11 at 19:05
  • Because I need to use the client's IP. I can not do without... This is where all my problems started :) – Ben Dec 07 '11 at 19:49

2 Answers2

0

Wont something like this help:

function getContentFromIframe(iFrameName)
{

    var myIFrame = document.getElementById(iFrameName);
    var content = myIFrame.contentWindow.document.body.innerHTML;

    //Do whatever you need with the content    

}


Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • This wouldn't work in my case because the content of the iFrame is: 1) XML and not HTML 2) From another domain, so I don't think I am allowed to access it this way. – Ben Dec 07 '11 at 19:07
  • The iFrame is loading content from a different domain. The Same-Origin policy prohibits accessing framed content from another domain, even if only to read it. – Aaron Dec 07 '11 at 19:11
  • Yea, skipped that, pls have a look at this: http://stackoverflow.com/questions/6474484/get-cross-domain-iframe-content – Sudhir Bastakoti Dec 07 '11 at 19:15
  • @Aaron is there no way to "bypass" that using HTML5 ? It should be possible somehow... No ? – Ben Dec 07 '11 at 19:45
  • @Sudhir Well, the post you suggested points that you need to have access to both sides, which is impossible in my case. – Ben Dec 07 '11 at 19:52
  • @BenCoriou So, if I understand you correctly, you're trying to load somebody else's content from a different domain in an iFrame and interact with its contents. In short: this is **not possible**. Same-Origin Policy dictates this. Even if you decided to use fragment messaging or postMessage to communicate with the frame, you'd need to put script in the other page that parsed the fragments from the querystring or received the message from the parent window. – Aaron Dec 07 '11 at 20:08
  • @aaron Yes, this si what I want to do. I simply need to use the client's IP instead of the servers. I'm really disapointed there's no way of doing this ... – Ben Dec 07 '11 at 20:12
  • And, just so we're clear, when you say 'client', you're referring to computer accessing your site in a web browser, correct? – Aaron Dec 07 '11 at 20:15
  • Yes, this is what I mean Aaron. I need the browser of the user to get the content, from the server it's useless. – Ben Dec 07 '11 at 20:16
0

This is still not possible and for good reason!

To read from another domain using the client's cookies, IP and credentials requires the page being viewed to expose the information somehow - It's a 2-way conversation with the Listener (Outer page) and Sender (IFrame) working together.

A reason this is required: Imagine making an IFrame that takes up 100% of the page. You could show a common website's login form in the IFrame and yet intercept keystrokes/input box changes and log them. The user would only know the difference if they checked the URL.

If you have control over the browser of the user, you could use GreaseMonkey's cross-domain AJAX to get the contents of the IFrame (Assuming Firefox/Chrome)

Basic
  • 26,321
  • 24
  • 115
  • 201