【发布时间】:2011-12-10 03:48:46
【问题描述】:
我怀疑这实际上是可能的,但我准备改正。
我需要能够从 iframe 中检测 iframe 的宽度和高度,因此如果 iframe src 是 iframeContent.html 我需要能够在此页面上显示值。
我无法控制承载 iframe 的页面,只能控制 iframe 的内容。
这可能吗?
【问题讨论】:
标签: javascript html iframe
我怀疑这实际上是可能的,但我准备改正。
我需要能够从 iframe 中检测 iframe 的宽度和高度,因此如果 iframe src 是 iframeContent.html 我需要能够在此页面上显示值。
我无法控制承载 iframe 的页面,只能控制 iframe 的内容。
这可能吗?
【问题讨论】:
标签: javascript html iframe
无论页面是在iframe 还是新窗口中加载,您始终可以获取网页尺寸,它始终以相同的方式工作。这是我发现用于获取window 尺寸的函数,您也可以使用它来获取<iframe> 尺寸。
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}
【讨论】:
现在这很简单:
<script>
console.log(innerWidth, innerHeight)
</script>
只需确保从 iframe 中加载此 JavaScript,它就会在浏览器的控制台中打印 iframe 窗口的宽度和高度。
【讨论】: