クロスドメイン iFrame があります。実際の iFrame は正常に機能し、ページを 100% の高さで表示します。
私の問題は、最初のページの高さが約 1200px で、iframe 内のリンクをクリックして次のページが 800px の場合、下部に 400px の空白スペースができることです。
したがって、iframe はコンテンツに合わせて大きくなりますが、縮小しません。以下は私のコードです:
iframe されるページに入るコード:
<!DOCTYPE html>
<head>
</head>
<body onload="parent.postMessage(document.body.scrollHeight, 'http://target.domain.com');">
<h3>Got post?</h3>
<p>Lots of stuff here which will be inside the iframe.</p>
</body>
</html>
iframe を受け取るページに入るコード:
<script type="text/javascript">
function resizeCrossDomainIframe(id, other_domain) {
var iframe = document.getElementById(id);
window.addEventListener('message', function(event) {
if (event.origin !== other_domain) return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}, false);
}
</script>
<iframe src='http://example.com/page_containing_iframe.html' id="my_iframe" onload="resizeCrossDomainIframe('my_iframe', 'http://example.com');">
</iframe>
javascriptを変更して縮小する方法を知っている人はいますか?
ありがとうございました。