Introduction of multiple methods for adaptive height of iframe frame

Source: Internet
Author: User

HTML code:

The code is as follows Copy Code

<iframe src= "http://www.fufuok.com/" id= "Iframepage" name= "Iframepage" frameborder=0 "Scrolling=no" width= "100%" Onload= "Iframeheight ()" ></iframe>


JavaScript code:

The code is as follows Copy Code


<script type= "Text/javascript" language= "JavaScript" >
function Iframeheight () {
var ifm= document.getelementbyid ("Iframepage");
var subweb = Document.frames? document.frames["Iframepage"].document:ifm.contentdocument;
if (IFM!= null && subweb!= null) {
Ifm.height = SubWeb.body.scrollHeight;
}
}
</script>

Code two

On the page containing the IFRAME:

The code is as follows Copy Code

<iframe src= "main.asp" width= "557px" height= "100%" name= "Mainv" id= "MAINV" scrolling= "no" marginheight= "0" Marginwidth= "0" frameborder= "0" style= "border: #FFFFFF" ></iframe>

And then on each page that you want to display in the IFRAME,

The code is as follows Copy Code

<body onload= "Parent.document.all.mainv.height=gettotalheight ();" >

And then add at the bottom of the page

The code is as follows Copy Code

<div id= "theend" style= "position:relative" ></div>
<script language= "JavaScript" >
function Gettotalheight () {
return document.getElementById ("TheEND"). offsettop+10;
}
</script>

The adaptive height of iframe under the same domain

The same domain under the parent page JS can get to the height of the iframe page, so after the IFRAME loaded to get the next height on the line

The code is as follows Copy Code
<iframe src = "./ue.html" id= "Iframe" frameborder= "0" scrolling= "no" style= "border:0px;width:1000px;" onload= "autoheight ();" ></iframe>
<script type= "text/javascript"
    function Autoheight () {
        var iframe = document.getElementById ("iframe");
        if (iframe. Document) {//ie own properties
            iframe.style.height = Iframe. Document.documentElement.scrollHeight;
       }else if (iframe.contentdocument) {//ie,firefox,chrome,opera,safari
            iframe.height = Iframe.contentDocument.body.offsetHeight;
       }
   }
</script>

If it is under the same domain name of different subdomains, set the next document.domain on the line

The IFRAME in IE6 and IE7 has no contentdocument attribute, and if the page in the IFRAME is in a different subdomain under the same domain, it passes through the IFRAME. Document.documentElement.scrollHeight Get the height is wrong, so it is recommended to use Iframe.contentwindow.document to get the height (added at 2010-03-22)

2. Cross-domain iframe adaptive height

Cross-domain, because of JS's homology strategy, the parent page of JS can not get to the height of the iframe page. Need a page to do the proxy.
The method is as follows: Suppose a page under www.a.com a.html to include a page under Www.b.com c.html.
We use another page under Www.a.com to act as proxies, to get the height of the IFRAME page and to set the height of the iframe element agent.html.

The a.html contains an iframe:

The code is as follows Copy Code

<iframe src= "http://www.b.com/c.html" id= "iframe" frameborder= "0" scrolling= "no" style= "border:0px"; ></iframe>

Add the following code to the c.html:

  code is as follows copy code

<iframe id=" c_iframe "  height=" 0 "width=" 0 "  src=" http:// Www.a.com/agent.html "style=" Display:none ></iframe>
<script type= "Text/javascript"
     (function Autoheight () {
        var b_width = Math.max ( Document.body.scrollwidth,document.body.clientwidth);
        var b_height = Math.max (Document.body.scrollHeight, Document.body.clientHeight);
        var c_iframe = document.getElementById ("C_iframe");
        c_iframe.src= ' # '
   } ();
</script>

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.