How to auto-size iframe in apex?

Member

by addison , in category: Third Party Scripts , 24 days ago

How to auto-size iframe in apex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 23 days ago

@addison 

To auto-size an iframe in an Apex application, you can use JavaScript to dynamically resize the iframe based on the content inside it. Here's a step-by-step guide to achieve this:

  1. Add an iframe element to your Apex page:
1
<iframe id="myIframe" src="https://www.example.com"></iframe>


  1. Add the following JavaScript code to your page to dynamically resize the iframe:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<script type="text/javascript">
$(document).ready(function() {
    var iframe = document.getElementById('myIframe');

    iframe.onload = function() {
        var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
        
        if (iframeWin.document.body) {
            iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
        }
    };
});
</script>


  1. Make sure to include the jQuery library in your Apex page if it's not already included:
1
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>


With this code in place, the iframe will automatically adjust its height based on the content inside it whenever the content changes. This approach ensures that the iframe is always correctly sized to fit its content.