javascript 从 iframe 访问父页面中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3886410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing variable in parent page from iframe
提问by amateur
I have a page with an iframe that contains a html page. I want to access a Javascript variable in the parent page from within the iframe. The name of the variable in the main page is observer.
我有一个包含 html 页面的 iframe 页面。我想从 iframe 中访问父页面中的 Javascript 变量。主页中变量的名称是observer.
I have tried this
我试过这个
parent.observer = 'aadasds';
but I am getting the following error:
但我收到以下错误:
Permission denied for to get property Window.observer from
获取属性 Window.observer 的权限被拒绝
.
.
采纳答案by tec
Exchanging values between iframes (and parent) is only allowed if both sites come from the same domain. If they do, your example should just work. If they don't, browsers inhibit the communication.
仅当两个站点都来自同一域时,才允许在 iframe(和父级)之间交换值。如果他们这样做,您的示例应该可以正常工作。如果他们不这样做,浏览器将禁止通信。
However there are a number of hacks to circumvent this: e.g the Yahoo.CrossFrame library described in Julien le Comte's blogusing a third iframe to enable one way communication, or the "resize an iframe around the iframe"-idea described in Adam Fortuna's blogenabling two way communication.
然而,有许多技巧可以规避这一点:例如,Julien le Comte 的博客中描述的 Yahoo.CrossFrame 库使用第三个 iframe 来启用单向通信,或者Adam Fortuna 的博客中描述的“围绕 iframe 调整 iframe 大小”的想法实现双向通信。
Edit(as people still seem to read this old answer):
In modern Browsers you can use postMessageto exchange Data between iframes. There are many javascript libraries that try to emulate that functionality in older browsers, too. E.g. by mis-using the location.hash, like the jquery-postmessage-plugindoes.
编辑(因为人们似乎仍在阅读这个旧答案):
在现代浏览器中,您可以使用postMessage在 iframe 之间交换数据。有许多 javascript 库也尝试在旧浏览器中模拟该功能。例如,误用 location.hash,就像jquery-postmessage-plugin那样。
回答by www139
It sounds as though your iframes are using different domains. All major browsers block access to parent iframes if they are not using the same domain. IE if you have the domain www.test.comand you embedded a page from www.google.comand try to access/modify anything from google's website, you will be denied access.
听起来好像您的 iframe 使用不同的域。如果它们不使用相同的域,所有主要浏览器都会阻止对父 iframe 的访问。IE 如果您拥有域www.test.com并且您嵌入了一个页面www.google.com并尝试访问/修改来自谷歌网站的任何内容,您将被拒绝访问。
The other answer to this question explains the implemented API post message. This can also be used to send/receive data from different frames from different domains. However, the things you can do with that are limited compared to if you had two frames using the same domain.
这个问题的另一个答案解释了实现的 API post message。这也可用于从不同域的不同帧发送/接收数据。但是,与使用相同域的两个框架相比,您可以使用它做的事情是有限的。
That being said, here is the answer if your iframes are using the same domain.
话虽如此,如果您的 iframe 使用相同的域,这就是答案。
window.parent.observer;
Hope this helps someone :)
希望这对某人有所帮助:)

