检测iframe内容高度变化

Nel*_*mel 13 javascript iframe

很多示例显示如何动态设置iframe其内容的高度.这对我来说很完美.我现在面临的问题是内容可以在不触发的情况下改变大小onload(想想隐藏/可扩展div的).

有没有办法检测iframe内容的大小何时发生变化?这是在同一个域,没有jQuery,请.

lon*_*day 14

我会通过定期轮询(可能每200毫秒,可能更频繁)使用来做到这一点setInterval.然后,您可以将内容的大小与上次的内容进行比较.

var iframe = document.getElementById('myIframe'),
    lastheight;

setInterval(function(){
    if (iframe.document.body.scrollheight != lastheight) {
        // adjust the iframe's size

        lastheight = iframe.document.body.scrollheight;
    }
}, 200);
Run Code Online (Sandbox Code Playgroud)