javascript、iframe、安全性-尝试从父窗口访问 js 函数时权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6094514/
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
javascript, iframe, security - Permission denied when tring to access a js function from parent window
提问by zozo
Good day to all.
祝大家有个美好的一天。
I have a page that includes an iframe. In that iframe I have a script with function called test(). I need to access the function from the parent window. After asking around I got to this solution:
我有一个包含 iframe 的页面。在那个 iframe 中,我有一个名为 test() 的函数的脚本。我需要从父窗口访问该函数。在四处询问后,我得到了这个解决方案:
<div onclick="document.getElementById('targetFrame').contentWindow.teste();">Test</div>
On click the test function should be run. The problem is that I get " Permission denied to access property test" error.
单击时应运行测试功能。问题是我收到“权限被拒绝访问属性测试”错误。
It looked like a permission error to me so I changed the file loaded in iframe permissions to 777, but without any result.
对我来说这看起来像是权限错误,所以我将 iframe 权限中加载的文件更改为 777,但没有任何结果。
Note: The file loaded in iframe is not on the same domain.
注意:iframe 中加载的文件不在同一个域中。
回答by bjornd
It's prohibited to access pages from other domains by default, because browsers use same origin policy. There are several workaround like using location.hash or window.name to communicate between frames. The most recent and standardized in HTML5 is postMessage-interface. There is library for cross-browser solution http://easyxdm.net/wp/.
默认情况下禁止访问来自其他域的页面,因为浏览器使用同源策略。有几种解决方法,例如使用 location.hash 或 window.name 在框架之间进行通信。HTML5 中最新和标准化的是 postMessage-interface。有跨浏览器解决方案的库http://easyxdm.net/wp/。
回答by Matej Janov?ík
I am not sure if it is possible, cross window (frame) communication have to be at same domain, protocol and hostname. For more info see Same origin policy for JavaScriptand Cross domain communication with iframes
我不确定是否可能,跨窗口(框架)通信必须在相同的域、协议和主机名中。有关更多信息,请参阅JavaScript 的同源策略和与 iframe 的跨域通信