跨站点脚本Iframe权限被拒绝的问题

Cross Site Scripting Iframe Permission Denied issue

本文关键字:拒绝 问题 权限 站点 脚本 Iframe      更新时间:2023-09-26

以下代码中出现跨站点脚本错误。

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:错误:访问属性"document"的权限被拒绝

Google Chrome:类型错误:无法读取未定义的属性"documentElement">

Internet Explorer:类型错误:权限被拒绝

对于"ee":

Mozilla Firefox:错误:访问属性"documentElement"的权限被拒绝

Google Chrome:类型错误:无法读取null的属性"documentElement">

Internet Explorer:错误:访问被拒绝

我认为它不能以一般的方式解决,因为它正在发生,因为域指向另一个域。因此,有人会指导我解决这个问题,而不使用Javascript contentDocument.documentElementcontentWindow.document.documentElement的这些属性来根据其内部内容动态调整Iframe内容的大小吗。

感谢

除了Christophe的回答之外,我还想指出(遗憾的是(postMessage并不能在所有浏览器上都工作。

幸运的是,Josh Fraser已经提供了一个向后兼容的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');

只要确保您正确阅读了文档,因为配置必须设置正确。

正如您所说,这是一个跨域问题。

如果您对两个页面都有控制权,则可以使用postMessage在两个页面之间交换信息。

一些参考文献:

  • Ben Alman调整iframe大小的示例
  • John Resig关于postMessaging的文章
  • 这篇关于iframes的精彩演讲(你是什么感兴趣的从幻灯片16开始(
  • 包含此技术的库/插件列表