Compatible multi-browser iframe adaptive height (1/2)

Source: Internet
Author: User

There are roughly two traditional approaches:
Method one, after each contained page in its own content loaded, execute JS to get the height of this page, and then to sync the parent page of the iframe height.
Method Two, in the main page iframe in the OnLoad event to execute JS, to get the content of the page contains the height, and then go to sync height.
In terms of code maintenance, method Two is better than method one, because method one, each included page is to introduce a piece of the same code to do this thing, create a lot of copies.

Two methods are only to deal with static things, that is, only when the content loading, if JS to operate the DOM caused by the height of changes, are not very convenient

<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ""
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>iframe Adaptive height </title>
<script type=" text/web Effects ">
function Reinitiframe () {
var iframe = document.getElementById ("frame_content");
try{
var bheight = iframe.contentwindow.document.body.scrollheight;
var dheight = iframe.contentwindow.document.documentelement.scrollheight;
var height = Math.max (bheight, dheight);
Iframe.height = height;
}catch (ex) {}
}
Window.setinterval ("Reinitiframe ()");

function Checkheight () {
var iframe = document.getElementById ("frame_content");
var bheight = iframe.contentwindow.document.body.scrollheight;
var dheight = iframe.contentwindow.document.documentelement.scrollheight;
Alert ("Bheight:" + bheight + ", Dheight:" + dheight);
}
</script>

<body>
<iframe src= "http://www.111cn.net" frameborder= "0" width= "100%" "scrolling=" No "id=" Frame_content "onload=" This.height=20 "allowtransparency=" true ></iframe>

Note ID and onload
Then add a JS
</body>

Tell me more about

iframe, especially without the border of the IFRAME because it can and the seamless combination of Web pages so as not to refresh the page in the case of some of the data to update the page is possible, but the size of the IFRAME is not like the layer can be "scalable", so that the use of trouble, to the IFRAME set Degree of time is not good, less is not, now, let me tell you an iframe dynamic adjustment of the height of the method, mainly the following JS function:

Program code

Function setwinheight (obj)
{
    var win=obj
    if documen T.getelementbyid)
    {
        if (Win &&! Window.opera)
        {
             if (win.contentdocument && win.contentdocument.body.offsetheight)

                 win.height = Win.contentdocument.body.offsetheight;
            Else if (win.document && Win.document.body.scrollheight)
                 win.height = win.document.body.scrollheight;
       }
   }
}

Finally, join the IFRAME, can not lose the onload attribute, of course, ID must also be in the function of the win match

Home 1 2 last page

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.