我正在寻找一个好的跨域iframe大小调整脚本,根据其内容调整其高度.我也可以访问html/css以获取iframe的来源.那里有没有?
tho*_*max 34
如果您的用户使用现代浏览器,则可以使用HTML5中的postMessage轻松解决此问题.这是一个很好的快速解决方案:
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>
Run Code Online (Sandbox Code Playgroud)
包含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>
Run Code Online (Sandbox Code Playgroud)
<iframe src='http://example.com/page_containing_iframe.html' id="my_iframe" onload="resizeCrossDomainIframe('my_iframe', 'http://example.com');">
</iframe>
Run Code Online (Sandbox Code Playgroud)
use*_*310 15
由于未能找到解决所有不同用例的解决方案,我最终编写了一个支持宽度和高度的简单js lib,在一个页面上调整内容和多个iframe的大小.
https://github.com/davidjbradshaw/iframe-resizer
| 归档时间: |
|
| 查看次数: |
30856 次 |
| 最近记录: |