[英]How to get height of iframe from its source
<iframe id="frame1" name="frame1" width="200" height="600" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://localhost/Widget/"></iframe>
我想从页面http://localhost/Widget/
使用JavaScript找到这个iframe的高度。
有没有办法这样做? 提前致谢。
我在自己的解决方案中对问题采取了不同的方式:在iframe中,我有:
<body onload="parent.setsize(document.body.scrollHeight);">
在父母中我有一个js函数:
function setsize(pixels){
if(navigator.appName=="Microsoft Internet Explorer") pixels+=40;
if (navigator.userAgent.indexOf("Firefox")!=-1) pixels+=40;
document.getElementById('produktet').style.height=pixels+"px";
如您所见,iframe内容的总高度为:document.body.scrollHeight
你必须先让“父母”这样做。 您可以通过document.parent
获取父级,因此以下内容应该可以工作(未测试):
parent.getElementById( 'frame1' ).getAttribute( 'height' );
如果您已在父级中加载jQuery,则可以使用:
parent.jQuery('#frame1').attr('height');
编辑 :请注意,这里有一个安全限制:两个页面必须在同一个域上才能工作。
绕过跨源问题。 您可以在身体加载前读取高度:
<html>
<head>
<script>
var iframeSize = Math.max( document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight );
</script>
</head>
<body>
...
$('#frame1').attr('height');
使用jQuery。 要么
document.getElementbyId('frame1').getAttribute('height');
用普通的js。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.