Home iframe自适应高度
Post
Cancel

iframe自适应高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<iframe src="backtop.html" frameborder="0" scrolling="no" id="test" οnlοad="this.height=100"></iframe>

<script type="text/javascript">
function reinitIframe () {
    var iframe = document.getElementById("test");
    iframe.height = 0; // 只有先设置原来的iframe高度为0,之前的iframe高度才不会对现在的设置有影响
    try{
        var bHeight = iframe.contentWindow.document.body.scrollHeight;
        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
        var height = Math.max(bHeight, dHeight);
        iframe.height = height;
    } catch (ex) {
    }
}
window.setInterval("reinitIframe()", 200);
</script>
This post is licensed under CC BY 4.0 by the author.