How to get the element inside an iframe element in Javascript?
Solution 1
You can ONLY interrogate content in an iframe if the content has the same protocol, domain and port number as the script that interrogates it. It is called SAME ORIGIN
If that is the case, then this code will show the content. If not - you cannot access the iframe from a normal script in a normal html page
Demo - tested in IE8, Chrome 13 and Fx6
function showIframeContent(id) {
var iframe = document.getElementById(id);
try {
var doc = (iframe.contentDocument)? iframe.contentDocument: iframe.contentWindow.document;
alert(doc.body.innerHTML);
}
catch(e) {
alert(e.message);
}
return false;
}
<iframe id='iframe_id1' src="javascript:parent.somehtml()"> </iframe>
<br/>
<a href="#" onclick="return showIframeContent('iframe_id1')">Show</a>
<hr/>
<iframe id='iframe_id2' src="http://plungjan.name/"> </iframe>
<br/>
<a href="#" onclick="return showIframeContent('iframe_id2')">Show</a>
<hr/>
Solution 2
Having:
var iframe = document.getElementById('iframe_id');
To get the content document you can use:
var contDoc = iframe.contentDocument || iframe.contentWindow.document;
Then you can search for your element inside the iframe by id.
Comments
-
Thomson almost 2 years
I have a element as below inside the document, I could get the iframe element by
document.getElementById('iframe_id')
, but how to get the element inside this iframe? I triediframeElement.contentWindow
, and the returnedDOMWindow
has no properties. Also triediframeElement.docuemnt
andiframeElement.contentDocument
, both of them are undefined. How can I get it? I am using the latest Chrome in my experiment.Here is the iframe element
<iframe id='iframe_id'> <html> <body>many content here</body> </html> </iframe>
-
Thomson over 12 yearsI suppose this should work. But I got contDoc undefined after I run your 2 statements. The returned iframe seems to be the right one.
-
Thomson over 12 yearsThe above sample html is just excerpt (simplified) from the Elements window at the bottom of Chrome. It shows that there is a full html inside the iframe. Do you mean this is fake in the UI?
-
mplungjan over 12 yearsYes. Anyway if the page is from the same server, then my code works. Please see update about same origin
-
Thomson over 12 yearsStill got
doc
undefine, very strange. -
Thomson over 12 yearsI just run your demo in my Chrome, and the the message box shows the right content in inside the html. So it could be some security issue that Chrome blocks to access the inner DOM since it comes from a different domain, as Tetaxa mentioned in the comment.
-
mplungjan over 12 yearsIt IS a security issue if the protocol, the domain or the port is different.
-
Răzvan Flavius Panda over 12 years@Thomson: It will work unless the iframe source is set to another domain.
-
mplungjan over 12 yearsI have updated the fiddle with code that might tell you where the iframe content comes from - it is crude but should give you an idea
-
rnrneverdies almost 9 yearsI'm getting Uncaught DOMException: Blocked a frame with origin "example.com" from accessing a cross-origin frame. any hint?
-
Răzvan Flavius Panda almost 9 years@oneway: If you own the iframe code, check workaround: stackoverflow.com/a/25098153/750216