简体   繁体   English

确定是否已加载iframe

[英]Determining if iframe has been loaded

I'm trying to figure out the size of an iframe's content so I can resize the iframe element to compass its content. 我试图弄清楚iframe内容的大小,以便调整iframe元素的大小以适应其内容。

How do I find out if the iFrame has been loaded and I can reliably measure it's content dimensions. 如何确定iFrame是否已加载,并且可以可靠地测量其内容尺寸。

Note: onload event won't do because the iframe can be loaded by the time onload is bound. 注意: onload事件不会执行,因为可以在绑定onload加载iframe。 Also, I'd like a vanilla JS solution not jQuery. 另外,我想要一个通用的JS解决方案而不是jQuery。

You could try this: 您可以尝试以下方法:

1- The iframe should have an ID: 1- iframe应该具有一个ID:

 <iframe id="slideshow_frame" src="frame.html" frameborder="0" width="100%" marginheight="0" marginwidth="0" scrolling="no"> </iframe> 

2- The page loaded into the iframe should contain all the html into an “easy retrievable” container (a div with an ID is perfect!): 2-加载到iframe中的页面应将所有html都包含到“易于检索”的容器中(具有ID的div是完美的!):

 <div id="content">    <div style="background: red; width: 400px; height: 120px"></div>    <div style="background: green; width: 400px; height: 300px"></div>    <div style="background: yellow; width: 400px; height: 60px"></div> </div> 

3- You can trigger this function wherever you want: 3-您可以随时随地触发此功能:

 function resizeIframe(iframeID) {      var iframe = window.parent.document.getElementById(iframeID); var container = document.getElementById('content'); iframe.style.height = container.offsetHeight + 'px';           } 

Source: 资源:

Adapting iframe height to its content with 2 lines of javascript 使用2行JavaScript将iframe的高度调整为其内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM