2011年7月22日 星期五

[JavaScript] Iframe根據內容大小自動調整長度

此篇為動態調整iframe內容的高度
還在為iframe高度得設死所苦惱嗎?
用用看這個吧~

iframe內容

<iframe id='iframe1' src='../test.jsp' width='95%' height='400' frameborder='0' onload='Javascript:SetCwinHeight(iframe1)'>


JavaScript內容

<script type="text/javascript">
/**iframe根據載入內容自動控制高度~ **/
function SetCwinHeight(sId){
    var bobo=document.getElementById(sId); //iframe id
    if (document.getElementById){
        if (bobo && !window.opera){
            if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){
                bobo.height = bobo.contentDocument.body.offsetHeight+50;
                if(bobo.height <= 350 ){
                    bobo.height = 350;
                }
            }else if(bobo.Document && bobo.Document.body.scrollHeight){
                bobo.height = bobo.Document.body.scrollHeight+50;
                    if(bobo.height <= 350 ){
                        bobo.height = 350;
                    }
                }
            }
       }
}
</script>

沒有留言:

張貼留言