Iframe adaptive height and width based on content

Source: Internet
Author: User

Iframe, especially Iframe without borders, is possible to update part of the page data without refreshing a new page because it can be seamlessly integrated with the webpage, however, the size of the Iframe is not as flexible as that of the layer, which makes it difficult to use. It is even worse to set the height of the Iframe.

JS get height

We can write the following functions and return the height of the Iframe framework.

Function SetCwinHeight (){
Var cwin = document. getElementById ("cwin ");
If (document. getElementById ){
If (cwin &&! Window. opera ){
If (cwin. contentDocument & cwin. contentDocument. body. offsetHeight)
Cwin. height = cwin. contentDocument. body. offsetHeight;
Else if (cwin. Document & cwin. Document. body. scrollHeight)
Cwin. height = cwin. Document. body. scrollHeight;
        }
    }
}
The final addition of Iframe cannot discard the Onload attribute. Of course, the ID must also be matched by the Cwin in the function.

<Iframe width = "778" align = "center" height = "200" id = "cwin" name = "cwin" onload = "Javascript: SetCwinHeight () "frameborder =" 0 "scrolling =" no "> </iframe>
Height of computing volume removal

Assume that our sub-page also has the following code on the page that is imported into the framework, and the name of this page is temp.html.

<Body onLoad = "parent. setSize (getSize ()">
<Table width = "100" height = "660" bgcolor = "# FFCC66">
<Tr>
<Td> </td>
</Tr>
</Table>
<Div id = "PageEnd" style = "position: relative"> </div>
<Script>
Function getSize (){
Return document. getElementById ("PageEnd"). offsetTop
}
</Script>
</Body>
We can write this on the home page.

<Script language = "JavaScript" type = "text/JavaScript">
Function setSize (theHeight ){
Document. all. ifrm. height = theHeight + 12;
}
</Script>
<Iframe id = "ifrm" width = "100%" height = "0px" scrolling = "no" src = "temp.htm"> </iframe>
Adaptive height

Assume that the page contains the iframe code as follows:

<Iframe id = "test" name = "test" frameborder = 0 scrolling = no src = "/u/1463420203" width = "100%" height = 0> </iframe>
Method 1

Add

<Script for = window event = onload language = "jscript">
Document. all ("test" 2.16.height1_test.doc ument. body. scrollheight;
</Script>
Adaptive width is similar to adding

Document. all ("test" 2.16.width+test.doc ument. body. scrollwidth;
Method 2

Create a bottom. js file and enter the following code (there are only two lines)

Parent.doc ument. all ("framework ID name"). style. height = document. body. scrollHeight;
Parent.doc ument. all ("framework ID name"). style. width = document. body. scrollWidth;
The framework ID here is the Iframe ID, for example:

<Iframe id = "framework ID name" name = "left" frameBorder = 0 scrolling = no src = "demo. asp" width = "100%"> </iframe>
Add all included files to your website

<Script language = "JavaScript" src = "bottom. js"/> </script>
Iframe adaptive height

Enter the list of iframe names that you want to automatically adjust the height based on the page height, and separate the IDs of each iframe with commas, for example, ["myframe1", "myframe2"], you can use only one form without a comma.

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

// 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. getElementByIdx_x ){
// Automatically adjust the iframe height
Dyniframe [dyniframe. length] = document. getElementByIdx_x (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. getElementByIdx_x) & iframehide = "no "){
Var tempobj = document. all? Document. all [iframeids [I]: document. getElementByIdx_x (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

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.