[英]jQuery Cross Domain Iframe Height Issue
I have the following javascript that determines the content height of an iframe: 我有以下用于确定iframe内容高度的JavaScript:
var body = document.body, html = document.documentElement;
var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
First time, this works great! 第一次,效果很好! Once i have the height i post it to the parent page, like so:
一旦达到高度,便将其发布到父页面,如下所示:
parent.postMessage(height, '*');
On the parent page i listen for the post message event and set the height of the iframe, like so: 在父页面上,我监听发布消息事件并设置iframe的高度,如下所示:
$('#frame').height(data);
So far so good! 到现在为止还挺好! Now when the iframe navigates within it self to a different page and it recalculates the content height, it keeps returning the height it calculated the first time.
现在,当iframe自身导航至其他页面并重新计算内容高度时,它会继续返回首次计算的高度。
I am assuming that this is because the iframe now has an explicit height. 我认为这是因为iframe现在具有明确的高度。 How do i ensure that i get the actual content height and not the height set on the iframe?
如何确保我得到的是实际的内容高度,而不是iframe上设置的高度?
You have to compute the body height on document.ready. 您必须在document.ready上计算主体高度 。
I suggest you to use easyXDM to get some fallback on postMessage. 我建议您使用easyXDM以获得postMessage的一些后备。 easyXDM has an example exactly about getting an iframe height .
easyXDM提供了一个有关获取iframe高度的示例 。
Thanks to Kevin B for the tip. 感谢Kevin B的提示。
Replaced: 替换为:
var body = document.body, html = document.documentElement;
var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
With: 带有:
document.getElementsByTagName('body')[0].offsetHeight
Works like a charm!!! 奇迹般有效!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.