iFrame 自动调整尺寸

问题描述 投票:0回答:1

在“死马当活马医”评论开始之前,让我先澄清一下我正在尝试做什么,看看是否有人可以帮助我。

我有一个带有 iFrame 的父窗口。 iFrame 中的内容是从一个单独的域加载的,我认为这是我的问题开始的地方,尽管它需要这样。当页面最初加载时,我运行以下内容:

<script language="javascript" type="text/javascript">
    var reportFrame = document.getElementById('report');

    function resizeIframe() {
        var height = document.documentElement.clientHeight;

        height -= reportFrame.offsetTop;

        // not sure how to get this dynamically
        height -= 20; /* whatever you set your body bottom margin/padding to be */

        reportFrame.style.height = height + "px";

    }

    reportFrame.onload = resizeIframe;
    window.onresize = resizeIframe;
    
</script>

这个效果很好。 iFrame 没有任何滚动条,看起来非常符合我的要求。我遇到的问题是 iFrame 有一个按钮会导致框架回发。我需要在 iFrame 重新加载自身时运行此脚本,以防止显示任何滚动条。由于子页面位于单独的域中,我无法从其向父页面调用函数(很确定这就是 XSS 发挥作用的地方),因此我需要想出另一种方式让父页面了解它需要运行此脚本,因为 iFrame 正在自行重新加载。

有什么建议吗?

javascript asp.net iframe dom-events
1个回答
2
投票

您可以添加一个计时器,定期检查并调整大小:

setInterval( "resizeIframe()", 1000 ); //check every second

[编辑 - 调整脚本大小的链接]

查看链接根据内容调整 iframe 大小,了解调整 iframe 大小的脚本。

© www.soinside.com 2019 - 2024. All rights reserved.