DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Auto Resizing Iframe

06.12.2006
| 13492 views |
  • submit to reddit
        I use this to visually integrate a Rails web app into an iframe within a Livlink intranet instance. YMMV.

<script type="text/javascript">
  URL = '/path/to/app';

  var dec = (typeof decodeURIComponent == 'function') ? decodeURIComponent : decode;
  function resize_ifrm() {
    var ifrm = document.getElementById('ifrm');
    if(window.frames['ifrm'] && window.frames['ifrm'].document) {
      window.frames['ifrm'].window.scroll(0,0);
      var body = window.frames['ifrm'].document.body;
      if(body) {
        ifrm.style.height = (body.scrollHeight || body.offsetHeight) + 'px';
      }
    }
  }
  src_checked = false;
  function check_src() {
    if(src_checked) return;
    src_checked = true;
    var ifrm = document.getElementById('ifrm');
    var urlPos = location.search.indexOf("iframeUrl=");
    if(urlPos > -1) {
      var src = dec(location.search.substring(urlPos+10));
    } else {
      var src = URL;
    }
    if(src != ifrm.src) ifrm.src = src;
  }
</script>

<iframe name="ifrm" id="ifrm" src="/blank.html"
  style="width:100%;height:100px;border:none;" scrolling="no" frameborder="0"
  onload="resize_ifrm();check_src();setInterval('resize_ifrm()', 1000);"></iframe>