When displaying content from external domains within iFrames, the same-origin policy poses a challenge in dynamically resizing those iFrames to accommodate their content.
One technique to bypass the cross-domain restriction involves exploiting a browser quirk that allows communication between pages in nested iFrame structures. In essence:
This structure allows for communication between:
In www.foo.com/home.html:
<script> function resizeIframe(height) { document.getElementById("frame_name_here").height = parseInt(height) + 60; } </script> <iframe>
In www.bar.com/framed.html:
<body onload="iframeResizePipe()"> <iframe>
In www.foo.com/helper.html:
<body onload="parentIframeResize()"> <script> function parentIframeResize() { var height = getParam("height"); parent.parent.resizeIframe(height); } function getParam(name) { var regex = new RegExp("[\?&]" + name + "=([^&]*)"); var results = regex.exec(window.location.href); if (results == null) return ""; else return results[1]; } </script>
This solution involves communication through a "helper" iframe (www.foo.com/helper.html) that resides on the same domain as the parent page and facilitates resizing requests between the child and parent iFrames.
The above is the detailed content of How Can I Dynamically Resize an iFrame Based on its Cross-Domain Content?. For more information, please follow other related articles on the PHP Chinese website!