错误:拒绝访问属性“文档”的权限

我不断收到错误"Error: Permission denied to access property 'document'"而我已经在我的X-FRAME options定义允许其他域,像这样..

   

下面是iframe请求的标题,清楚地显示我已经定义允许域访问iframe但不能正常工作。 我想要的是使用javascript调整iframe的大小。

在此处输入图像描述

这是我调整iframe高度的javascript代码。

   function iframeLoaded() { var iFrameID = document.getElementById('idIframe'); if(iFrameID) { iFrameID.height = ""; if(iFrameID.contentWindow.document.body.scrollHeight < 500) { iFrameID.height = "500px"; } else { iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px"; } } }  

我怎样才能做到这一点? 请建议。

我最近自己有这个问题。 最后我用postMessage方法解决了它。

  1. 在iFrame中包含的文档中,我检查它是否实际上是从iFrame运行的。

     function inIframe(){ if(top != self){ var contentHeight = $('#myIframeContent').height(); //Just this part I did with jQuery as I was sure that the document uses it postSize(contentHeight); } } 
  2. 如果文档在iFrame中运行,请调用我们将调用postSize的函数。

     function postSize(height){ var target = parent.postMessage ? parent : (parent.document.postMessage ? parent.document : undefined); if(typeof target != "undefined" && document.body.scrollHeight){ target.postMessage(height, "*"); } } 
  3. 将以下代码添加到包含iFrame的文档中

     function receiveSize(e){ if(e.origin === "http://www.mywebsite.net"){ var newHeight = e.data + 35; document.getElementById("myIframeID").style.height = newHeight + "px"; } } window.addEventListener("message", receiveSize, false); 

不幸的是,我不记得所有这些的确切来源,因为我在Stackoverflow上查看了很多不同的解决方案,但也有不同的网站。 但这个解决方案对我有用。

干杯

不确定为什么其他解决方案使用iFrame ID。 它应该没有:

iFrame中向外部发送消息的页面:

  

父框架:

 window.addEventListener("message", receiveMessage, false); function receiveMessage(event) { console.log('Message: ', event.data); } 

更新compatability.js到最新版本为我修复了它。