Adjust width and height of iframe to fit with content in it

前端 未结 30 2333
旧时难觅i
旧时难觅i 2020-11-22 02:09

I need a solution for auto-adjusting the width and height of an iframe to barely fit its content. The point is that t

30条回答
  •  孤街浪徒
    2020-11-22 02:34

    This is a solid proof solution

    function resizer(id)
    {
    
    var doc=document.getElementById(id).contentWindow.document;
    var body_ = doc.body, html_ = doc.documentElement;
    
    var height = Math.max( body_.scrollHeight, body_.offsetHeight, html_.clientHeight, html_.scrollHeight, html_.offsetHeight );
    var width  = Math.max( body_.scrollWidth, body_.offsetWidth, html_.clientWidth, html_.scrollWidth, html_.offsetWidth );
    
    document.getElementById(id).style.height=height;
    document.getElementById(id).style.width=width;
    
    }
    

    the html

    
    

提交回复
热议问题