获取到 iframe 的页面高度

core functions

/*
    * When the iframe is on a different subdomain, uncomment the following line
    * and change "example.com" to your domain.
    */
// document.domain = "example.com";
function setIframeHeight(iframe) {
    if (iframe) {
        var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
        if (iframeWin.document.body) {
            iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
        }
    }
};

use way

window.onload = function () {
    setIframeHeight(document.getElementById('external-frame'));
};
<iframe onload="setIframeHeight(this);" ></iframe>

踩坑

要确保被iframe 的页面本身的 body 是有高度的,不然被调用后的值是获取不到的(在一些采用绝对定位的内嵌页面就有可能存在这个情况)

© DeveWork.com 2018. sitemap  统计 Updated at 2020-02-24 10:54

results matching ""

    No results matching ""