0

I've had a browse around similar posts but not found anything that is really related to my scenario.

I have a summary page in which I iframe several pages that the user has travelled through on route to the summary page. In each of the pages that are being iframed. I'm using .toggle to expand/collapse the content of the page.

The issue I'm finding is that I'm using a script on the summary page when it is first loaded to make the iframe the size of the content of the page it is including, but when I click to toggle that content and collapse it, it leaves the iframe at the same size so that there is a huge chunk of whitespace.

I'm guessing that I need to send something back from the included page to the summary page that says the content size has changed and to therefore re-size the iframe size but am unsure how to do this.

Can someone please point me in the right direction?

An example of the files is...

Parent.html

<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript">        
    // Resize an iframe so it's as tall as its contents. This could be much shorter but is expanded for clarity.
    function size_frame(frame_name, frame_id)
    {
       // Get the iframe element
       var frame_element = document.getElementById(frame_id);           
       // Get the iframe frame-window object. Note this has different properties to frame_element.
       var frame_window = frames[frame_name];           
       // Set the height to 1px first, because some browsers will give you the maximum height of either the
       // frame or its contents, when you try to read the scrollHeight property as we do below.
       frame_element.style.height = "1px";           
       // set the height of the iframe element to the "scrollHeight" of the document element of the frame window.
       frame_element.style.height = frame_window.document.documentElement.scrollHeight +"px";           
    }    
</script>
<h1>SUMMARY</h1>
<table border="0" cellspacing="0" cellpadding="0" width="850">
    <tr>
        <td><iframe src="child1.html" width="850" frameborder="0" scrolling="no" name="child1" id="child1" onload="size_frame('child1','child1');"></iframe></td>
    </tr>
</table>

Child.html:

<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript">
    $().ready(function () {
        $('th[event-type-header]').bind('click', function () {
            var eventType = $(this).attr('event-type-header');
            $('tr[event-type="' + eventType + '"]').toggle('hide-event-type');
        });
    });
</script>
<h1>Child Page</h1>

<table width="800" border="0" cellspacing="0" cellpadding="0" class="content">
    <tr>
        <th class="content" event-type-header="childPage">
            Header <img src="arrow_down.gif" align="right" width="15" />
        </th>
    </tr>
    <tr class="content2" event-type="childPage">
        <td align="center">
            <table width="750" cellpadding="0" cellspacing="0" class="content">
                <tr>                        
                    <th class="content" colspan="2">Sub Heading</th>
                </tr>
                <tr class="content2">
                    <td>
                        Some Text           
                    </td>
                </tr>                           
            </table>
        </td>
    </tr>
</table>

CPB07
  • 649
  • 2
  • 12
  • 23
  • Please include some code, to make your question clearer. – Boaz Apr 09 '13 at 10:56
  • Just call a function in the parent window instance that adjusts the iframe size …? – CBroe Apr 09 '13 at 11:35
  • This problem has plagued many, but there is a workaround: http://stackoverflow.com/questions/153152/resizing-an-iframe-based-on-content – Mike H. Apr 09 '13 at 13:54

1 Answers1

0

You need to use offsetHeight, rather than scrollHeight.

David Bradshaw
  • 10,116
  • 3
  • 33
  • 61