JavaScript control iframe Adaptive Height example

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.