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