当iframe的内容动态变化时(例如:ajax加载),调整iframe的大小

Resize iframe when its content dynamically changes (ex: ajax load)

本文关键字:iframe 加载 ajax 调整 动态 变化 例如      更新时间:2023-09-26

当iframe的url源高度发生变化时,我可以使用以下代码调整iframe的大小:

<iframe src="source" width="625" height="100%" name="CHANGETHIS" id="CHANGETHIS" marginheight="0" frameborder="0" onLoad="autoResize('CHANGETHIS');"></iframe>
<script language="JavaScript">
    function autoResize(id){
        var newheight;
        var newwidth;
        if(document.getElementById){
            newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
            newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
        }
        document.getElementById(id).height= (newheight) + "px";
        document.getElementById(id).width= (newwidth) + "px";
    }
</script>

但是我不知道当它的内容由于动态变化而改变时,我该如何调整它的大小。例如,jQuery动态更改。是否有触发这个autoResize()函数来避免滚动出现在导航期间,因为这个动态内容的变化?

注意:我可以操纵框架站点。

谢谢

var autoResEl = autoResize('#myElement');
setInterval( autoResEl , 400 );