Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get iframe body after load?

Tags:

jquery

iframe

I need obtain the iframe body after iframe load

like image 937
user207123 Avatar asked Nov 10 '10 22:11

user207123


People also ask

How to get body content of an iframe using JavaScript?

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.

Why is my iframe being killed before it is loaded?

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.

What are the advantages of iframe?

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.

Why can't I access the contents of an iframe?

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.


2 Answers

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.

like image 157
PleaseStand Avatar answered Oct 21 '22 08:10

PleaseStand


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.

like image 1
Maksim Vi. Avatar answered Oct 21 '22 07:10

Maksim Vi.