检测< iframe> iframe中的高度和宽度 [英] Detect <iframe> height and width from inside the iframe
问题描述
我怀疑这实际上是可能的,但林准备纠正。
I doubt this is actually possible but Im prepared to be corrected.
我需要的是能够从内部检测iframe的宽度和高度iframe,所以如果iframe src
是 iframeContent.html
我需要能够表达这个页面上的值。
What I need is to be able to detect the width and height of an iframe from within the iframe, so if the iframe src
is iframeContent.html
I need to be able to surface the values on this page.
我无法控制托管iframe的页面仅包含iframe的内容。
I have no control over the page that hosts the iframe only the contents of the iframe.
这是可以吗?
推荐答案
无论页面是否加载到 iframe
或者一个新窗口,它总是以相同的方式工作。这是我发现用于获取 window 维度的函数,您也可以使用它获取
< iframe>
You can always get the web page dimensions, no matter if the page is loaded inside an iframe
or a new window it always works the same way. This is a function I've found used to get window
dimensions, you can also use it to get an <iframe>
dimensions.
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 );
}
- source (感谢Andy E添加链接)
- jsFiddle示例
- source (thanks Andy E for adding the link)
- jsFiddle example
这篇关于检测< iframe> iframe中的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!