错误:拒绝访问属性“文档”的权限 [英] Error: Permission denied to access property 'document'

查看:218
本文介绍了错误:拒绝访问属性“文档”的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到错误错误:拒绝权限访问属性'document',而我已在我的 X-FRAME中定义选项允许其他域名,如此..

I am continuously getting the error "Error: Permission denied to access property 'document'" while i have already define in my X-FRAME options to allow the other domain, like this..

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

以下是iframe请求的标头,清楚地显示我已定义允许域访问iframe但没有工作。我想要的是使用javascript调整iframe的大小。

Below is the header of iframe request, clearly shows i have defined to allow the domain to access the iframe but not working. All i want is to resize the iframe using javascript.

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

Here is my javascript code to resize the iframe height.

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

我该怎么做?请建议。

推荐答案

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

I very recently had this issue myself. Finally I solved it with the postMessage method.


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

  1. In the document included to the iFrame I check whether it's actually running from an 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);
         }
    }


  • 如果文档在iFrame中运行,调用我们将调用postSize的函数。

  • If the document is running within an iFrame, call a function that we will call 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, "*");
            }
        }
    


  • 将以下代码添加到文档中包括你的iFrame

  • Add the following code to the document that includes your 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上查看了很多不同的解决方案,但也有不同的网站。但这个解决方案对我有用。

    Unfortunately I cannot remember the exact sources of all this as I was viewing a lot of different solutions here on Stackoverflow, but also different websites. But this solution works good for me.

    干杯

    这篇关于错误:拒绝访问属性“文档”的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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