javascript - how to extract the value of a field in an iframe into the main page - Stack Overflow

I have an iframe inserted into my main page.The iframe generates a value which is of the type hiddenI n

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?

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?

Share Improve this question asked Mar 23, 2009 at 13:50 kartikkowligikartikkowligi
Add a ment  | 

5 Answers 5

Reset to default 3

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.

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.

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.";

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.

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

If your page and the iframe content e 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 pliant or secure...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744401013a4572386.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信