How to Realize the adaptive height of iframe (embedded frame)

Source: Internet
Author: User

Several times I was asked how to achieve the auto-adaptive height of iframe. I was able to automatically adapt to the length of the page so as to avoid the simultaneous appearance of the scroll bar in addition to the page and iframe, I encountered a similar problem in my work, so I went online to review the problem and copied it to another place. I got such a function and pasted it on the page. Not exclusive. If you find it useful, please use it.

The source code is as follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// ** Iframe automatically adapts to the page **//

// Enter the list of iframe names that you want to automatically adjust the height based on the page height
// Use commas (,) to separate the IDs of each iframe. For example, ["myframe1", "myframe2"] can have only one form.

// Define the iframe ID
Var iframeids = ["test"]

// If your browser does not support iframe, whether to hide iframe: yes, no
Var iframehide = "yes"

Function dyniframesize ()
{
Var dyniframe = new Array ()
For (I = 0; I <iframeids. length; I ++)
{
If (document. getElementById)
{
// Automatically adjust the iframe height
Dyniframe [dyniframe. length] = document. getElementById (iframeids [I]);
If (dyniframe [I] &! Window. opera)
{
Dyniframe [I]. style. display = "block"
If (dyniframe [I]. contentDocument & dyniframe [I]. contentDocument. body. offsetHeight) // if your browser is NetScape
Dyniframe [I]. height = dyniframe [I]. contentDocument. body. offsetHeight;
Else if (dyniframe [I]. Document & dyniframe [I]. Document. body. scrollHeight) // if your browser is IE
Dyniframe [I]. height = dyniframe [I]. Document. body. scrollHeight;
}
}
// Display the browser that does not support iframe Based on the set parameters
If (document. all | document. getElementById) & iframehide = "no ")
{
Var tempobj = document. all? Document. all [iframeids [I]: document. getElementById (iframeids [I])
Tempobj. style. display = "block"
}
}
}

If (window. addEventListener)
Window. addEventListener ("load", dyniframesize, false)
Else if (window. attachEvent)
Window. attachEvent ("onload", dyniframesize)
Else
Window. onload = dyniframesize
</Script>


You only need to paste it in

Related Article

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.