Search code examples
jqueryiframe

How get iframe body after load?


I need obtain the iframe body after iframe load


Solution

  • document.getElementById('frame').contentDocument.body will give you it in pure JavaScript, assuming that the iframe's ID is frame. In jQuery, that would be $('#frame').contents().find('body').

    Note that because of the same-origin policy, this will only work if the iframe and the surrounding pages' URLs have exactly the same hostname and port number.

    Edit: You commented that you get an "Access denied" error when you tried this code. The same-origin policy is why this happens. The excellent Browser Security Handbook has information about this. A close reading of that web page will suggest that there are ways to break through, though this is not possible unless you control the enclosed site because of security and privacy reasons.