Dynamically adjust an iframe's height

纵饮孤独 提交于 2019-12-22 08:54:51

问题


I have an iframe that contains some content from a site. I want the iframe to adjust to 100% of the src content's height. Bit of a noob at js - here's what I'm working with:

  <iframe id="frame" scrolling="no" frameborder="0" src="http://www.srcwebsite.org"></iframe>

    <script type="text/javascript">
    function resizeIframe() {
        var height = document.documentElement.clientHeight;
        height -= document.getElementById('frame').offsetTop;

        height -= 20; /* whatever you set your body bottom margin/padding to be */

        document.getElementById('frame').style.height = height +"px";

    };
    document.getElementById('frame').onload = resizeIframe;
    window.onresize = resizeIframe;
    </script>

This works great to resize the height of the iframe to its page, but I want it to resize it to the height of the src...


回答1:


You can't access the height of the iframe's content because of Same Origin Policy (protocol, domain and ports must match).

You could set up CORS, keep in mind it's not supported in IE until IE8.



来源:https://stackoverflow.com/questions/7563408/dynamically-adjust-an-iframes-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!