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

标签 javascript html wordpress iframe cross-domain

我在以下代码中遇到跨站脚本错误。

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.documentElementcontentWindow.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/

相关文章:

html - 网页导出为缺少 Microsoft word css

javascript - 从 HTML 表单文本区域值和 Post 中用 JavaScript 制定 JSON?

Wordpress:为什么会多次调用 init Hook ?

javascript - 有没有办法通过 "undefine"实例方法来揭示原型(prototype)中定义的方法?

javascript - 如果内容太长,使用 JS 更改/设置 div 高度

javascript - 从 Javascript 函数返回 JSON 对象

javascript - Uncaught ReferenceError : eve is not defined

HTML 输入模式只有 1 个空格

html - 修复了动态标题图像上的可见高度值

mysql - ACF 按转发器字段的子字段过滤自定义帖子