views:

639

answers:

6

Hi,

I have an iframe inserted into my main page.

The iframe generates a value which is of the type hidden

I need to extract this value into my main page using a javascript

Can someone please help me with this?

+2  A: 

You can access the contents of the iframe from the parent page the following way:

var iframe = document.getElementById("iframeId");
var field = iframe.contentWindow.document.getElementById("hiddenFieldId");

I didn't test it and it's been a while since I've used raw DOM in JavaScript so let me know if it does not work.

DrJokepu
+3  A: 

If the page that you load in the iframe is in the same domain of your main page, you can access the DOM of that page with:

 window.frames[iframeName].document

and than it's very easy to get the value of any element.

alexmeia
+1  A: 

If the url of the iframe is to a different domain then you might need to make sure the domain of the document in the iframe is the same as the domain of the containing document. Run this code in both documents before trying to read the values of the field:

document.domain = "example.com";

If the content of the iframe belongs to a different site than yours, then you do not have access to it with javascript. This is a security measure.

Marius
A: 

Hi friend,

Use this
fram.document.getElementById("name").value

Sure it will work

Details :

fram = id given to the frame
name = id given to the text box inside the frame

praveenjayapal
A: 

If your page and the iframe content come from different domains, this will be quite tricky. I was recently forced to find a workaround for this and managed pull it off with an AJAX call to a PHP script utilizing file_get_contents() but it is not very copyright compliant or secure...

mike nvck
A: 

how is possible to get an the value from such element when it has javascript inside?