[英]Iframe setting dynamic height works only on chrome
I'm using an iframe
on my webpage. 我在网页上使用
iframe
。 I need the set the height of the iframe
dynamically, according to the content. 我需要根据内容动态设置
iframe
的高度。 I'm using the following code to achieve this. 我正在使用以下代码来实现这一目标。
$('iframe').on('load',function(){
$('iframe').attr('id','sop_iframe');
$('#sop_iframe').height($('#sop_iframe').contents().height())
});
This works perfectly in chrome.. but not on firefox and IE. 这在chrome中可以完美运行..但在Firefox和IE上则不能。 Is there any cross browser jquery solution??
有任何跨浏览器的jQuery解决方案吗?
function setFrameHeight() {
var h;
var defaulth = 150;
var scrollh = window.top.document.documentElement.scrollTop;
//reset the iframe to default height
window.top.document.getElementById("iframe_form").height = defaulth;
//get actual heights
var y1 = document.documentElement.scrollHeight;
if (y1 < document.body.scrollHeight) y1 = document.body.scrollHeight;
var y2 = document.documentElement.offsetHeight;
if (y2 < document.body.offsetHeight) y2 = document.body.offsetHeight;
h = (y1 > y2) ? y1 : y2;
//determine smaller heights
h = (h > defaulth) ? h : defaulth;
//set "iframe_form" to proper height
window.top.document.getElementById("iframe_form").height = h;
//scroll to top
window.top.scrollTo(0,scrollh);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.