[英]Error: Permission denied to access property 'document'
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.. 我不断收到错误
"Error: Permission denied to access property 'document'"
而我已经在我的X-FRAME options
定义允许其他域,像这样..
<?php
header('X-Frame-Options: ALLOW-FROM http://mydomain.com');
?>
Below is the header of iframe request, clearly shows i have defined to allow the domain to access the iframe but not working. 下面是iframe请求的标题,清楚地显示我已经定义允许域访问iframe但不能正常工作。 All i want is to resize the iframe using javascript.
我想要的是使用javascript调整iframe的大小。
Here is my javascript code to resize the iframe height. 这是我调整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>
How can i do this? 我怎样才能做到这一点? Please suggest.
请建议。
I very recently had this issue myself. 我最近自己有这个问题。 Finally I solved it with the postMessage method.
最后我用postMessage方法解决了它。
In the document included to the iFrame I check whether it's actually running from an iFrame. 在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); } }
If the document is running within an iFrame, call a function that we will call postSize. 如果文档在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, "*"); } }
Add the following code to the document that includes your iFrame 将以下代码添加到包含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);
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. 不幸的是,我不记得所有这些的确切来源,因为我在Stackoverflow上查看了很多不同的解决方案,但也有不同的网站。 But this solution works good for me.
但这个解决方案对我有用。
Cheers 干杯
Not sure why the other solutions use an iFrame ID. 不确定为什么其他解决方案使用iFrame ID。 It should also work without:
它应该没有:
Page within the iFrame which sends a message to outside: iFrame中向外部发送消息的页面:
<script>
parent.postMessage('your message', '*');
</script>
Parent frame: 父框架:
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
console.log('Message: ', event.data);
}
更新compatability.js到最新版本为我修复了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.