I need obtain the iframe body after iframe load
To get body content from javascript ,i have tried the following code: var frameObj = document.getElementById ('id_description_iframe'); var frameContent = frameObj.contentWindow.document.body.innerHTML; where "id_description_iframe" is your iframe's id. This code is working fine for me.
what it does is stall out the server because the report is being created by php, but then all of a sudden the iframe is killed before it’s actually loaded. Load the script that generates the report into the top-level window instead of the IFRAME and see if it appears as expected.
The sites or webpages loaded using iframe maintain their session history and cache values and are not dependent on the parent or the calling browser screen. At times we may need to execute a function once the iFrame content has loaded.
Important note: You can't access the contents of an iFrame if it's cross-domain. See Same-Origin policy. What I find wired is that browsers don't recognize as same origin (so do not authorize to work together) two files I put into the same directory on a local drive.
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.
Because of security reasons there is no way to get iframe content with javascript if it is on different domain. Take a look at JSONP instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With