Topic on Extension talk:IframePage

Autofit frame size to contents

4
Roips (talkcontribs)

Added the following line inside SpecialIframePage.php to cause the iframe's size to fit its contents:

'onload' => 'document.getElementById(\'iframeframe\').style.height = (this.contentWindow.document.body.offsetHeight + 20) + \'px\''

Don't forget to add a comma because it's inside an array.

The full snippet is:

$html .= Html::element( 'iframe',

array( 'id' => "iframe-$srcName",

'src' => $src,

'frameborder' => '0',

'width' => '100%',

'height' => '100%',

'onload' => 'document.getElementById(\'iframeframe\').style.height = (this.contentWindow

.document.body.offsetHeight + 20) + \'px\''

)

);

P.S.: It's mainly used to put Apache's Directory Listing inside a frame at my wiki pages

Smason3346 (talkcontribs)

If the iframe is on a different domain, then you may get a console error saying the access to document (on the iframe) is restricted. That is built into the browsers to prevent cross-site scripting attacks.

To get around this I edited my SpecialIframePage.php to allow a height parameter.

Add this line (under where the $path gets assigned)

$height = $request->getVal( 'height' );

$height = ( empty($height) ) ? "100%" : $height;

Finally put the height as a dynamic parameter:

$html .= Html::element( 'iframe',

                array( 'id' => "iframe-$srcName",

                'src' => $src,

                'frameborder' => '1',

                'width' => '100%',

                'height' =>  $height                

                )

So now my call on the page that includes the iframe looks like this:

{{Special:IframePage | Name-Of-Iframe | height=950px}}

And if you don't enter the height as a parameter it uses what was the default before.

71.46.232.141 (talkcontribs)

This didn't work for me. The height never changed. Is there something else that needs to be done?

Yokiria (talkcontribs)

Had the same problem, so I'm replying here how I solved it.

The create iframe has a min-height property by default (500px I think), si you need to override it if you need less than that.


Here's my code :

$html .= Html::element( 'iframe',

               array( 'id' => "iframe-$srcName",

               'src' => $src,

               'frameborder' => '1',

               'width' => '100%',

               'height' =>  $height ,

              'style' => "height: $height; min-height: 100px"

               )

Reply to "Autofit frame size to contents"