iframe 自适应高度

/**
         * 页面撑开
         */
        function iframeAutoFit()
        {
            try
            {
                if (window != parent)
                {
                    var a = parent.document.getElementById("myiframe");
                    if (a.contentWindow == window)
                    {
                        var h1 = 0, h2 = 0;
                        a.parentNode.style.height = a.offsetHeight + "px";
                        a.style.height = "10px";
                        if (document.documentElement && document.documentElement.scrollHeight)
                        {
                            h1 = document.documentElement.scrollHeight;
                        }
                        if (document.body) h2 = document.body.scrollHeight;

                        var h = Math.max(h1, h2);


                        if (document.all) {
                            h += 0;
                        }
                        if (window.opera) {
                            h += 1;
                        }
                        a.style.height = a.parentNode.style.height = h + "px";
                    }
                }
            }
            catch (ex) {
            }
        }
        if (window.attachEvent)
        {
            window.attachEvent("onload", iframeAutoFit);
        }
        else if (window.addEventListener)
        {
            window.addEventListener('load', iframeAutoFit, false);
        }
    </script>

<iframe id="iFrame1" name="iFrame1" width="100%" onload="this.height=iFrame1.document.body.scrollHeight" frameborder="0" src="index.htm"></iframe>
看到了吧,关键就在于onload="this.height=iFrame1.document.body.scrollHeight"!

<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>

function SetWinHeight(obj)
{
var win=obj;
if (document.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;
}
}
}


 


<script language="javascript" type="text/javascript">
function dyniframesize(down) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(down);
}
else{
eval('pTar = ' + down + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"
if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax
pTar.height = pTar.contentDocument.body.offsetHeight +20;
pTar.width = pTar.contentDocument.body.scrollWidth+20;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax
pTar.height = pTar.Document.body.scrollHeight;
pTar.width = pTar.Document.body.scrollWidth;
}
}
}
</script>
<iframe src ="/default2.aspx" frameborder="0" marginheight="0" marginwidth="0" frameborder="0" scrolling="auto" id="ifm" name="ifm" onload="javascript:dyniframesize('ifm');" width="100%">
</iframe>


<html>
<head>
<script>
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight + 20; //FF NS
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight + 10;//IE
}
else
{
if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
}
}
}
</script>
</head>
<body>
<iframe src="20103622440.html" onload="SetCwinHeight(this);" width="600px">
</body>
</html>

原文地址:https://www.cnblogs.com/amylis_chen/p/2690550.html