Full-screen iframe with a height of 100%

前端 未结 17 1672
星月不相逢
星月不相逢 2020-11-22 04:35

Is iframe height=100% supported in all browsers?

I am using doctype as:



        
17条回答
  •  庸人自扰
    2020-11-22 05:18

    Only this worked for me (but for "same-domain"):

    function MakeIframeFullHeight(iframeElement){
        iframeElement.style.width   = "100%";
        var ifrD = iframeElement.contentDocument || iframeElement.contentWindow.document;
        var mHeight = parseInt( window.getComputedStyle( ifrD.documentElement).height );  // Math.max( ifrD.body.scrollHeight, .. offsetHeight, ....clientHeight,
        var margins = ifrD.body.style.margin + ifrD.body.style.padding + ifrD.documentElement.style.margin + ifrD.documentElement.style.padding;
        if(margins=="") { margins=0;  ifrD.body.style.margin="0px"; }
        (function(){
           var interval = setInterval(function(){
            if(ifrD.readyState  == 'complete' ){
                iframeElement.style.height  = (parseInt(window.getComputedStyle( ifrD.documentElement).height) + margins+1) +"px";
                setTimeout( function(){ clearInterval(interval); }, 1000 );
            } 
           },1000)
        })();
    }
    

    you can use either:

    MakeIframeFullHeight(document.getElementById("iframe_id"));
    

    or