ITeye博客

IFrame自适应高与contentWindow

leyoo · 2010-10-11 10:20 · javascript · 编程语言 · Markdown

IFrame自适应高与contentWindow

一定要加contentWindow, 不则是当前的document.body.scrollHeight

contentWindow Property Internet Development Index


Retrieves the window object of the specified frame or iframe.

Syntax

HTML N/A Scripting document.all.object.contentWindow

Possible Values

oWindow Object that receives the window object.

The property is read-only. The property has no default value.

Remarks

This property is useful if you do not know the id of the frame or iframe you are accessing through a collection.

=============================================

其子窗口中加入以下代码即可

autoFixIframe: function(){
    try
    {
        if(window!=parent)
        {
            var commentIfrm = parent.document.getElementById("yocityAd");
            if(commentIfrm.contentWindow==window)
            {
                var h1=0, h2=0, d=document, dd=d.documentElement;
                commentIfrm.parentNode.style.height = commentIfrm.offsetHeight +"px";
                commentIfrm.style.height = "10px";

                if(dd && dd.scrollHeight) h1=dd.scrollHeight;
                if(d.body) h2=d.body.scrollHeight;
                var h=Math.max(h1, h2);

                if(document.all){h += 4;}
                if(window.opera){h += 1;}
                commentIfrm.style.height = commentIfrm.parentNode.style.height = h +"px";
            }
            
        }
    }
    catch (ex){}
}



if(window.attachEvent)
{
    window.attachEvent("onload",  iframeAutoFit);
}
else if(window.addEventListener)
{
    window.addEventListener('load',  iframeAutoFit,  false);
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。