javascript、iframe、安全性-尝试从父窗口访问 js 函数时权限被拒绝 [英] javascript, iframe, security - Permission denied when tring to access a js function from parent window

查看:14
本文介绍了javascript、iframe、安全性-尝试从父窗口访问 js 函数时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有一个包含 iframe 的页面.在那个 iframe 中,我有一个名为 test() 的函数的脚本.我需要从父窗口访问该函数.在四处询问后,我得到了这个解决方案:

Test

单击时应运行测试功能.问题是我收到权限被拒绝访问属性测试"错误.

在我看来像是权限错误,所以我将 iframe 中加载的文件权限更改为 777,但没有任何结果.

注意:iframe中加载的文件不在同一个域中.

解决方案

默认情况下禁止访问来自其他域的页面,因为浏览器使用 同源策略.有几种解决方法,例如使用 location.hash 或 window.name 在框架之间进行通信.HTML5 中最新和标准化的是 postMessage-interface.有跨浏览器解决方案的库http://easyxdm.net/wp/.

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:

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

Note: The file loaded in iframe is not on the same domain.

解决方案

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

这篇关于javascript、iframe、安全性-尝试从父窗口访问 js 函数时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆