在获取从 Iframe 调用的页面高度时,我遇到了非常奇怪的错误。我浏览了谷歌,还检查了与此“访问属性文档的权限被拒绝”相关的其他 Stackoverflow 帖子,但尚未找到任何解决方案。我有一个网站指向另一台服务器。我在 Iframe 上收到此错误。让我提供代码
Jquery
$(document).ready(function()
{
// Set specific variable to represent all iframe tags.
var iFrames = document.getElementsByTagName('iframe');
// Resize heights.
function iResize()
{
// Iterate through all iframes in the page.
for (var i = 0, j = iFrames.length; i < j; i++)
{
// Set inline style to equal the body height of the iframed content.
iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
}
}
// Check if browser is Safari or Opera.
if ($.browser.safari || $.browser.opera)
{
// Start timer when loaded.
$('iframe').load(function()
{
setTimeout(iResize, 0);
}
);
// Safari and Opera need a kick-start.
for (var i = 0, j = iFrames.length; i < j; i++)
{
var iSource = iFrames[i].src;
iFrames[i].src = '';
iFrames[i].src = iSource;
}
}
else
{
// For other good browsers.
$('iframe').load(function()
{
// Set inline style to equal the body height of the iframed content.
this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
}
);
}
}
);
和 IFrame
<iframe style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe>
我收到“访问属性文档的权限被拒绝”错误,因此高度不符合其中的文档高度。然后我尝试了以下代码
function resizeIframe(ifRef)
{
var ifDoc;
//alert(ifRef);
try
{
ifDoc = ifRef.contentWindow.document.documentElement;
}
catch( e )
{
try
{
ifDoc = ifRef.contentDocument.documentElement;
}
catch( ee ){}
}
var doc = ifRef.height;
//alert(doc);
if(ifDoc)
{
ifRef.height = 1;
ifRef.style.height = ifDoc.scrollHeight+'px';
}
}
和 iframe
<iframe onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe`>
在这种情况下,我发现 javascript 没有错误,但它不起作用。但奇怪的是,它们都在我的本地服务器上工作,并且当代码所在的服务器现在没有指向另一个服务器时也在工作。(跨站点脚本)。
请帮我解决这个问题。
我使用的引用资料
error : Permission denied to access property 'document'
Error document.form is undefined in javascript
最佳答案
这称为 XSS 异常。错误:错误:访问属性“文档”的权限被拒绝
不幸的是,您想要做的事情——在与父域不同的站点上操作 iframe 的内容或窗口——是不允许的。期间。
关于javascript - 获取 IFrame 子项高度时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15036935/