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

前端 未结 30 2385
旧时难觅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:56

    Here is a cross-browser solution if you don't want to use jQuery:

    /**
     * Resizes the given iFrame width so it fits its content
     * @param e The iframe to resize
     */
    function resizeIframeWidth(e){
        // Set width of iframe according to its content
        if (e.Document && e.Document.body.scrollWidth) //ie5+ syntax
            e.width = e.contentWindow.document.body.scrollWidth;
        else if (e.contentDocument && e.contentDocument.body.scrollWidth) //ns6+ & opera syntax
            e.width = e.contentDocument.body.scrollWidth + 35;
        else (e.contentDocument && e.contentDocument.body.offsetWidth) //standards compliant syntax – ie8
            e.width = e.contentDocument.body.offsetWidth + 35;
    }
    

提交回复
热议问题