最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论