父頁面獲取iframe子頁面的高度


function setIframeHeight(id){
try{
var iframe = document.getElementById(id);
if(iframe.attachEvent){
iframe.attachEvent("onload", function(){
iframe.height = iframe.contentWindow.document.documentElement.scrollHeight;
});
return;
}else{
iframe.onload = function(){
iframe.height = iframe.contentDocument.body.scrollHeight;
};
return; 
} 
}catch(e){
throw new Error('setIframeHeight Error');
}
}

  

 

涉及了一些兼容問題:

IE用attachEvent | 3C用onload來判斷子頁面是否加載完成。

IE用contentWindow | 3C用contentDocument來獲取子頁面

IE用document.documentElement.scrollHeight(兼容ie6 ie7)| 3C用body.scrollHeight獲取頁面高度

 

 

子級頁面給父級頁面元素設置高度

function setParentIframeHeight(id){
    try{
        var parentIframe = parent.document.getElementById(id);
         if(window.attachEvent){
            window.attachEvent("onload", function(){
                parentIframe.height = document.documentElement.scrollHeight;
            });
            return;
        }else{
            window.onload = function(){
                parentIframe.height = document.body.scrollHeight;
            };
            return;                 
        }     
    }catch(e){
        throw new Error('setParentIframeHeight Error');
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM