【发布时间】:2013-02-09 06:11:37
【问题描述】:
我在以下代码中遇到跨站点脚本错误。
Javascript
function resizeIframe(ifRef)
{
var ifDoc;
//alert(ifRef);
try
{
ifDoc = ifRef.contentWindow.document.documentElement;
}
catch( e )
{
alert(e);
try
{
ifDoc = ifRef.contentDocument.documentElement;
}
catch( ee ){
alert(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>
错误如下
对于“e”:
Mozilla Firefox:错误:访问属性“文档”的权限被拒绝
谷歌浏览器:TypeError:无法读取未定义的属性“documentElement”
Internet Explorer:TypeError: Permission denied
对于“ee”:
Mozilla Firefox:错误:访问属性“documentElement”的权限被拒绝
谷歌浏览器:TypeError:无法读取 null 的属性“documentElement”
Internet Explorer:错误:访问被拒绝。
我认为它无法以一般方式解决,因为它正在发生,因为域指向另一个域。所以有人会指导我解决它,而不使用 Javascript contentDocument.documentElement 或 contentWindow.document.documentElement 的这些属性来根据其内部内容动态调整 iframe 内容的大小。
谢谢
【问题讨论】:
-
只是检查:这些是不同的域,还是同一个域中的不同子域?
标签: javascript html wordpress iframe cross-domain