title: Change iframe height based on dynamic content inside
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/153152/resizing-an-iframe-based-on-content">Resizing an iframe based on content</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to load one of my pages into an iframe. I'm never sure how big the page will be, the data on the page is loaded dynamically. I'd like the iframe to always fit the page, no matter how big or small it is. Here's what I have:</p>
  
  <pre><code>function loadModal() {
      myframe = $('&lt;iframe id="mymodal" src="MyPage.aspx" width="700"&gt;&lt;/iframe&gt;');
  
      myframe.appendTo($('html'));
  
      var height = document.getElementById('modalPreview').contentWindow
                       .document.body.scrollHeight;
  
      $("#mymodal").attr("height", height);
  }
  </code></pre>
  
  <p>I've been trying to get the height of the page after it's loaded. The problem is that <code>height</code> comes back as <code>0</code>. But if I do this:</p>
  
  <pre><code>    setTimeout(function () {
          $("#mymodal").attr("height", height);
      }, 2000);
  </code></pre>
  
  <p>the correct height is loaded. I assume it's because the data needs a few seconds to load. But this looks funky if the page loads really fast, or it will still give me a height of 0 if it takes more than 2 seconds to load the page.</p>
  
  <p>So is there a way to:</p>
  
  <ol>
  <li>Wait and set the height of the iframe once the data loads, or</li>
  <li>Set the height of the parent iframe from <code>MyPage.aspx</code>?</li>
  </ol>