1

If i was to visit the source of my iframe, I notice that it has a value "srt" , I can access this variable by simply typing "srt" When in the iframes Source. However when it is an "iframe" typing "srt" no longer works since the variable is nested within the iframe.

How can i store the value of a variable that is in an iframe, To a variable that is not In the iframe, to access this value Outside.

4

2 回答 2

2

您可以按索引或框架名称使用框架集合。集合中的每个元素都是该window框架的对象,它是存储全局变量的地方。

frames[0].srt // if there's just one
frames['frameName'].srt  // accessing by the name property

或者您可以获取 iframe 元素并使用contentWindow检索 iframe 的窗口对象

 document.getElementById('frameId').contentWindow.srt;

另请记住,您无法访问不在同一域中的框架。

于 2013-10-30T21:24:13.097 回答
1

假设 iframe 有一个 id frame,来自父级:

var innerValue = document.getElementById('frame').contentWindow['srt'];

或者反过来,从 iframe 内部:

parent.iframeValue = srt;
于 2013-10-30T21:24:31.680 回答