Welcome to %s forums

<No. 1 Community for developers, by developers/>

Login Register

How to Resize Iframe to Fit Content? (Same Domain Only)

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
admin
Site Admin
Posts: 45

How to Resize Iframe to Fit Content? (Same Domain Only)

Post by admin » Thu Aug 22, 2019 2:48 pm

Make sure your site is running with jQuery then apply below code:

Code: Select all

<script type='text/javascript'>
    
    $(function(){
    
        var iFrames = $('iframe');
      
        function iResize() {
        
            for (var i = 0, j = iFrames.length; i < j; i++) {
              iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';}
            }
            
            if ($.browser.safari || $.browser.opera) { 
            
               iFrames.load(function(){
                   setTimeout(iResize, 0);
               });
            
               for (var i = 0, j = iFrames.length; i < j; i++) {
                    var iSource = iFrames[i].src;
                    iFrames[i].src = '';
                    iFrames[i].src = iSource;
               }
               
            } else {
               iFrames.load(function() { 
                   this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
               });
            }
        
        });

</script>
HTML:

Code: Select all

<iframe src="content.html" class="iframe" scrolling="no" frameborder="0"></iframe>

Post Reply