javascript - 获取 IFrame 子项高度时出错

标签 javascript jquery wordpress iframe

在获取从 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

http://davidwalsh.name/iframe-permission-denied

https://sqa.stackexchange.com/questions/1453/how-to-fix-permission-denied-to-access-property-document

http://sahi.co.in/forums/discussion/2898/error-permission-denied-to-access-property-document-with-ckeditor/p1

http://www.codingforums.com/showthread.php?t=236484

最佳答案

这称为 XSS 异常。错误:错误:访问属性“文档”的权限被拒绝

不幸的是,您想要做的事情——在与父域不同的站点上操作 iframe 的内容或窗口——是不允许的。期间。

关于javascript - 获取 IFrame 子项高度时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15036935/

相关文章:

javascript - 从 Nightmare.js 返回 HTML 正文

javascript - 如何在 react 中使用 if else 语句验证 RegExp?

javascript - __proto__ 在 JavaScript 中仅显示一个继承

WordPress 休息 API : How do I append all ACF of a Post Object?

wordpress - Woocommerce 运费计算逻辑

javascript - WordPress热门帖子如何显示帖子之类的类别?

javascript - 如何检测 angularJS 中的引导日期时间选择器更改事件

javascript - Jquery Fancybox 如何添加额外的导航按钮?

javascript - Div 在调整大小/突出显示之前不会出现

javascript - 用于 jQuery 的 vs10 智能感知