jQuery 错误:权限被拒绝访问 iFrame 中的属性“文档”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23508925/
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
Error: Permission denied to access property 'document' in iFrame
提问by Julian Dormon
I have a parent html page which uses jquery to append a dynamic iframe which has a source to another domain. I have already set the X-Frame-Options to allow this and loading plain content is not a problem. In fact all my other scripts seem to be working fine.
我有一个父 html 页面,它使用 jquery 附加一个动态 iframe,该 iframe 具有另一个域的源。我已经将 X-Frame-Options 设置为允许这样做并且加载纯内容不是问题。事实上,我所有的其他脚本似乎都运行良好。
The html page which loads in the iFrame, uses the jQuery DirtyForms pluginwhich has several lines which look like $(document). Both the dirty forms code and jQuery scripts are local to the iframe's document.
在 iFrame 中加载的 html 页面使用jQuery DirtyForms 插件,该插件有几行看起来像$(document)。脏表单代码和 jQuery 脚本都是 iframe 文档的本地代码。
When the any of these lines executes, I receive the following error in Firefox ( I have not tested any other browsers), Error: Permission denied to access property 'document'.
当这些行中的任何一行执行时,我在 Firefox 中收到以下错误(我没有测试过任何其他浏览器),错误:访问属性“文档”的权限被拒绝。
I have been reading up on this and this seems to be a problem if the parent page is trying to access the child's scripts or visa-versa, but in my case, the scripts that are erring are local to the child.
我一直在阅读这个,如果父页面试图访问孩子的脚本或反之亦然,这似乎是一个问题,但在我的情况下,出错的脚本是孩子本地的。
BTW. The child code, when NOT loaded within the iFrame, works perfectly.
顺便提一句。子代码,当未加载到 iFrame 中时,可以完美运行。
I am at a loss right now to find a work-around.
我现在不知所措,无法找到解决方法。
I assume that $(document) is somehow being evaluated as the parent.
我假设 $(document) 以某种方式被评估为父级。
Is there some way of correcting this behavior? Perhaps by modifying the plug-ins code. Perhaps, $(document).find('contentWindow')...??
有没有办法纠正这种行为?也许通过修改插件代码。也许,$(document).find('contentWindow')...??
I appreciate the help! Thanks.
我感谢您的帮助!谢谢。
回答by Neel
Well you cant access it..Now the question is why?
那么你不能访问它..现在的问题是为什么?
For that just go through below links
为此,只需通过以下链接
Permission denied to access property 'document'
Error: Permission denied to access property 'document'
And in ur case if u want to do anything on page load then better use below code for IFrame
在你的情况下,如果你想在页面加载时做任何事情,那么最好使用下面的 IFrame 代码
<iframe ........ onload='yourFunction( this )'>
回答by NightOwl888
In Dirty Forms 1.x, the default behavior attaches event handlers to anchor tags in any parent page that the current page is hosted within. The code that does that looks like:
在 Dirty Forms 1.x 中,默认行为将事件处理程序附加到当前页面所在的任何父页面中的定位标记。执行此操作的代码如下所示:
$(top.document).on('click', 'a', aBindFn);
$(top.document).on('submit', 'form', formBindFn);
That is, it is attaching event handlers to the window.top.document
. You can turn off that behavior by using:
也就是说,它将事件处理程序附加到window.top.document
. 您可以使用以下方法关闭该行为:
$.DirtyForms.watchParentDocs = false;
In Dirty Forms 2.x, this behavior has been removed because it pertained only to one specific use case of iFrames (hosting the form that used Dirty Forms within an iFrame). You can now customize event bindingto attach event handlers either to parent (host) pages or to child iFrames as needed by your page, but by default there is no interaction with iFrames. This should allow you to be able to work around issues such as this one.
在 Dirty Forms 2.x 中,此行为已被删除,因为它仅与 iFrame 的一个特定用例有关(在 iFrame 中托管使用 Dirty Forms 的表单)。您现在可以自定义事件绑定以根据页面需要将事件处理程序附加到父(主机)页面或子 iFrame,但默认情况下不与 iFrame 交互。这应该使您能够解决诸如此类的问题。