我在以下代码中遇到跨站脚本错误。
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 onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe>
错误如下
对于“e”:
Mozilla Firefox:错误:访问属性“文档”的权限被拒绝
Google Chrome:TypeError:无法读取未定义的属性“documentElement”
Internet Explorer:类型错误:权限被拒绝
对于“ee”:
Mozilla Firefox:错误:访问属性“documentElement”的权限被拒绝
Google Chrome 浏览器:类型错误:无法读取 null 的属性“documentElement”
Internet Explorer:错误:访问被拒绝。
我认为它不能以一般方式解决,因为它正在发生,因为域指向另一个域。那么有人会指导我解决它而不使用 Javascript contentDocument.documentElement
或 contentWindow.document.documentElement
的这些属性来根据其内部内容动态调整 Iframe 内容的大小吗.
谢谢
最佳答案
除了Christophe的回答,我想(遗憾地)指出 postMessage
不适用于所有浏览器。
幸运的是,Josh Fraser已提供a backwards compatible version of window.postMessage() .它检查浏览器是否支持 postMessage
方法。如果是这样,它会使用它。如果没有,它会使用 URL(来自 iframe 和父级)来传递数据。
现在您可以使用以下方法让两个窗口互相“对话”:
XD.postMessage(msg, src, frames[0]);
XD.receiveMessage(function(message){
window.alert(message.data + " received on "+window.location.host);
}, 'URL');
请确保您正确阅读了文档,因为必须正确设置配置。
关于javascript - 跨站点脚本 Iframe 权限被拒绝问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15044292/