Javascript 如何从源获取iframe的高度
我想从页面中找到此iframe的高度http://localhost/Widget/ 使用JavaScript 有什么办法吗?提前谢谢Javascript 如何从源获取iframe的高度,javascript,html,iframe,Javascript,Html,Iframe,我想从页面中找到此iframe的高度http://localhost/Widget/ 使用JavaScript 有什么办法吗?提前谢谢 <iframe id="frame1" name="frame1" width="200" height="600" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://localhost/Widget/"></iframe> 使用jQu
<iframe id="frame1" name="frame1" width="200" height="600" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://localhost/Widget/"></iframe>
使用jQuery。或
$('#frame1').attr('height');
用简单的js
使用jQuery。或
$('#frame1').attr('height');
对于纯js。您必须首先让父级执行此操作。您可以通过document.parent获取父级,因此以下操作不应经过测试:
document.getElementbyId('frame1').getAttribute('height');
如果已在父级中加载jQuery,则可以使用:
parent.getElementById( 'frame1' ).getAttribute( 'height' );
编辑:请注意,这里有一个安全限制:两个页面必须在同一个域中才能工作。您必须首先让父页面这样做。您可以通过document.parent获取父级,因此以下操作不应经过测试:
document.getElementbyId('frame1').getAttribute('height');
如果已在父级中加载jQuery,则可以使用:
parent.getElementById( 'frame1' ).getAttribute( 'height' );
编辑:请注意,这里有一个安全限制:两个页面必须在同一个域上才能工作。在我自己的解决方案中,我对这个问题采取了不同的方式: 在iframe中,我有:
parent.jQuery('#frame1').attr('height');
正如您所看到的,iframe内容的总高度为:document.body.scrollHeight我在自己的解决方案中采用了不同的方法: 在iframe中,我有:
parent.jQuery('#frame1').attr('height');
如您所见,iframe内容的总高度为:document.body.scrollHeight,以绕过跨原点问题。您可以读取车身加载前的高度:
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";
绕过跨来源问题。您可以读取车身加载前的高度:
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";
不安全的javascript尝试,如果iframe违反相同的原始版本,则可能重复不安全的javascript尝试如果iframe违反相同的原始版本,则可能重复不安全的javascript尝试