Brian Ortuño
Brian Ortuño

Reputation: 11

Read the source code of an iframe on another domain

I need read a source code content an iframe, this is a cross domain

  <script type="text/javascript">
     $(document.getElementById("iFrame")).ready(function(){
     var iframe = document.getElementById('iFrame');
     var innerDoc = iframe.contentDocument;
    });

Any chance to read the source code of my iframe on another domain?

Upvotes: 1

Views: 551

Answers (2)

Louis Ricci
Louis Ricci

Reputation: 21106

There's HTML5 postMessage and there's URL Hash polling (for browsers that don't support HTML5).

https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

http://softwareas.com/cross-domain-communication-with-iframes


If you don't have control of both domains, then you're stuck with a server side solution. Send an ajax request to your in-domain server with the URL as a parameter and have it return the HTML.


EDIT Because of the downvotes I'll elaborate on URL Hash polling a bit. Despite not having read access to the URL because of cross-domain restriction you still have access to set the URL Hash (text after the # character at the end of the URL). So you set the hash, which doesn't cause a page reload and the script can 'poll' for changes to that hash.

Upvotes: 1

Jimmy Sawczuk
Jimmy Sawczuk

Reputation: 13614

As a commenter points out, this is a cross domain request so it's not going to work as an AJAX request. You might try making an endpoint on your own domain that you can submit an AJAX request to which in turn retrieves the content from the URL server-side.

Upvotes: 1

Related Questions