iframe のコンテンツを制御できるため、ウィンドウメッセージイベントを試すことができます。
これをフレーム コンテンツに追加します。
// post a message to the top window with information about the height
window.onload = function(){
top.postMessage(document.body.scrollHeight, '*');
}
そして、これを iframe を含むページに:
// listen for the message and set the iframe height when it arrives
window.addEventListener( "message", function(event){
document.getElementById('test').style.height = event.data+'px';
}, false);
<iframe id="test" src="test.html"/>
あなたのコメントに応えて。iframe のコンテンツが変更されたときに iframe の高さを更新する方法をいくつか紹介します。
1. 突然変異イベント
// listen to all descendants of the body and if there are any modifications, post an update to the top window
document.body.addEventListener("DOMSubtreeModified", function(){
top.postMessage(document.body.scrollHeight, '*');
}, false);
そのアプローチは減価償却されています。これは代替品ですが、まだ完全にはサポートされていません:
2. 突然変異観察者
// create a MutationObserver to listen for modification to the body and post an update to the top window
var observer = new MutationObserver(function(){
top.postMessage(document.body.scrollHeight, '*');
});
observer.observe(document.body, {childList: true, subtree: true});
2番目のオプションがより適切にサポートされるまで、最初のオプションを使用します