Javascript 获取IFrame子对象的高度时出错

Javascript 获取IFrame子对象的高度时出错,javascript,jquery,wordpress,iframe,Javascript,Jquery,Wordpress,Iframe,在从Iframe获取调用的页面高度时,我遇到了一个非常奇怪的错误。我浏览了谷歌,还查看了其他与“拒绝访问财产文档的权限”相关的Stackoverflow帖子,但还没有找到任何解决方案。我有一个指向另一个服务器的网站。我在Iframe上得到了这个错误。让我提供代码 Jquery $(document).ready(function() { // Set specific variable to represent all iframe tags. var i

在从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 onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe`> 

在本例中,我没有发现javascript错误,但它不起作用。但奇怪的是,它们都在我的本地服务器上工作,而且在代码所在的服务器没有指向另一个服务器时也在工作。(跨站点脚本)

请帮我解决这个问题

我使用的参考资料


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


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

指的是哪个字符串错误<代码>iFrames[i].contentWindow.document.?我想你的页面上有一些国外的iFrame,是由一些社交网络插件或类似的东西制作的,最好是申请自己的iframes特殊类并使用
document.getElementsByClassName()
你能给我举个例子吗。我在Jquery方面很差。虽然不应该有任何外国Iframe,因为我制作的网站中没有使用社交网络插件。我在contentWindow.document部分遇到错误。所以我使用了第二个脚本resizeIframe(ifRef)。但还是不起作用。正如我之前所说的,第一个工作正常。域转移后,它停止工作。我只是使用了上述方式,再次被拒绝访问属性“document”的权限错误检查
控制台。警告('SRC',iFrames[i].SRC)(将其添加到给出错误的行之前);打开js控制台和WATHC错误不会出现在
iFrames[i].style.height=iFrames[i].contentWindow.document.body.offsetHeight+'px'零件,而是在零件上
this.style.height=this.contentWindow.document.body.offsetHeight+'px';是的,你是对的。那么,除了使用ajax而不是Iframe,我可能别无选择。或者你可以给我任何其他的想法,在没有
contentWindow.document.documentElement
contentDocument.documentElement
ajax调用跨域的情况下获取Iframe父对象的高度:P。。。除非您调用一个服务器端脚本来进行XSS调用,或者您有一个JSONp钩子,那么解决方案是什么呢?请您给我介绍一下好吗?这是所有现代浏览器的安全功能。你是不允许这样做的。那么,谁又能实现这些东西呢?
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 onload="resizeIframe(this)" style="margin-bottom: 16px;" src="ourteamnav/first.php" frameborder="0" scrolling="no" width="597" height="240"></iframe`>