IFRAME has a annoying problem, is not set the height of the case, the nested area will show a large blank, and sometimes set a height is not good, and later use JavaScript easy to solve the problem of IFRAME adaptive height, the following experience and specific methods to share.
In order to demonstrate convenience, here need 2 pages, one is the page of the IFRAME, one is the Mother page, first look at the Mother page, that is, a page with IFRAME code:
| The code is as follows |
Copy Code |
<title>iframe Adaptive Load Page height </title> <body> <iframe src= "iframe.htm" ></iframe> </body>
|
The page being iframe: Suppose the filename is: iframe.htm, the code in this file is:
| The code is as follows |
Copy Code |
| <title>iframe Adaptive Web Height </title> <script type= "Text/javascript" > function Iframeautofit () { var ex; Try { if (window!=parent) { var a = Parent.document.getElementsByTagName ("IFRAME"); for (var i=0; i<a.length; i++)//author:meizz { if (A[i].contentwindow==window) { var h1=0, h2=0; if (document.documentelement&&document.documentelement.scrollheight) { H1=document.documentelement.scrollheight; } if (document.body) h2=document.body.scrollheight; var H=math.max (H1, H2); if (document.all) {h = 4;} if (window.opera) {h = 1;} A[i].style.height = h + "px"; } } } } catch (ex) {} } if (document.attachevent) { Window.attachevent ("onload", Iframeautofit); Window.attachevent ("OnResize", Iframeautofit); } Else { Window.addeventlistener (' Load ', Iframeautofit, false); Window.addeventlistener (' Resize ', Iframeautofit, false); } </script> <body> <table border= "1" width= "style=" height:400px; Background-color:yellow "> <tr> <td>iframe the height of the Adaptive Load page (multi-browser compatible, support XHTML) </td> </tr> </table> </body> |
Finally add one: the height/width of the IFRAME, adaptive content.
| The code is as follows |
Copy Code |
<mce:script language= "Javascript" ><!--
Window.onload=function () { Parent.document.all ("Iframe1"). Style.height = Document.body.scrollHeight; Parent.document.all ("Iframe1"). Style.width = Document.body.scrollWidth; } --></mce:script> |
Note that in the Iframe.htm page, to use JS to do some settings, this is more critical, if you do not understand it does not matter, you can directly copy the code to your page can be iframe.