简体   繁体   English

动态调整iframe的高度

[英]Dynamically adjust an iframe's height

I have an iframe that contains some content from a site. 我有一个包含网站内容的iframe。 I want the iframe to adjust to 100% of the src content's height. 我希望iframe调整到src内容高度的100%。 Bit of a noob at js - here's what I'm working with: 在js的一个菜鸟 - 这是我正在使用的:

  <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... 这非常适合将iframe的高度调整为其页面,但我希望它将其大小调整为src的高度...

You can't access the height of the iframe 's content because of Same Origin Policy (protocol, domain and ports must match). 由于同源策略 (协议,域和端口必须匹配),您无法访问iframe内容的高度。

You could set up CORS , keep in mind it's not supported in IE until IE8. 您可以设置CORS ,请记住它在IE中不支持IE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM