[英]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>
I want to find the height of this iframe from the page http://localhost/Widget/
using JavaScript. 我想从页面
http://localhost/Widget/
使用JavaScript找到这个iframe的高度。
Is there any way to do so? 有没有办法这样做? Thanks in advance.
提前致谢。
I go a different way about the problem in my own solution: Within the iframe I have: 我在自己的解决方案中对问题采取了不同的方式:在iframe中,我有:
<body onload="parent.setsize(document.body.scrollHeight);">
In the parent I have a js function: 在父母中我有一个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";
As you can see the total height of the iframe content is: document.body.scrollHeight 如您所见,iframe内容的总高度为:document.body.scrollHeight
You'll have to get the "parent" first to do so. 你必须先让“父母”这样做。 You can get the parent by
document.parent
, so the following should work (not tested): 您可以通过
document.parent
获取父级,因此以下内容应该可以工作(未测试):
parent.getElementById( 'frame1' ).getAttribute( 'height' );
If you've loaded jQuery in the parent, you might use that: 如果您已在父级中加载jQuery,则可以使用:
parent.jQuery('#frame1').attr('height');
EDIT : Do note however, that there is a security restriction in play here: both pages have to be on the same domain for this to work. 编辑 :请注意,这里有一个安全限制:两个页面必须在同一个域上才能工作。
To bypass cross origin issues. 绕过跨源问题。 You can read the height before the body loads:
您可以在身体加载前读取高度:
<html>
<head>
<script>
var iframeSize = Math.max( document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight );
</script>
</head>
<body>
...
$('#frame1').attr('height');
using jQuery. 使用jQuery。 Or
要么
document.getElementbyId('frame1').getAttribute('height');
with plain js. 用普通的js。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.