praveenjayapal
praveenjayapal

Reputation: 38529

Retrieve the content of an iframe

I have a window, with an iframe in it. When i clicking a button, then the iframe will load (loaded from a web site). Is it possible to retrieve the content of that iframe? How can I retrieve the content of that page?

I can view the source code by left clicking iframes View Source.

I need to store the source in a DB.

1. var iframe = document.getElementById("ifrm");

alert(iframe.contentWindow.document.body.innerHTML );

2.alert(window.frames['ifrm'].document.body.innerHTML);

these two comments are showing "access denied" error.

Please help me.

Upvotes: 0

Views: 1029

Answers (2)

Ryan Emerle
Ryan Emerle

Reputation: 15811

This should work (if it's on the same domain):

document.getElementById('iframeId').contentWindow.document.body.innerHTML

Note that you need to make sure the frame has been loaded completely before you run this code. Otherwise, you will get strange exceptions.

Disclaimer: I have not tested this.

Here's the google query I used: google query

Upvotes: 3

phihag
phihag

Reputation: 287755

It is generally not possible to read the contents of an iframe loaded from another server, due to the same origin security policy. However, it looks like you want to store the content on the server anyway, so why not request the content from the server?

Upvotes: 1

Related Questions