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

Cod*_*ous 6 javascript php jquery x-frame-options

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

 <?php
        header('X-Frame-Options: ALLOW-FROM http://mydomain.com'); 
    ?>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

<iframe src="http://mydomain.com/xxx/yyy" id="idIframe" onload="iframeLoaded();" allowtransparency="true" frameborder="0" width="100%" scrolling="no"></iframe>
<script type="text/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";
          }
    }   
}
</script>
Run Code Online (Sandbox Code Playgroud)

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

ent*_*ull 15

我最近自己有这个问题.最后我用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);
             }
        }
    
    Run Code Online (Sandbox Code Playgroud)
  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, "*");
            }
        }
    
    Run Code Online (Sandbox Code Playgroud)
  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);
    
    Run Code Online (Sandbox Code Playgroud)

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

干杯