Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trigger an event whenever an HTML page gets longer?

I have a page with an iframe. The iframe contents sometimes do dynamic JS things that cause them to grow. When thta happens, I have to resize the iframe to make sure it's long enough, which I do with this, inside the iframe:

// I saved you the boredom of all the try{}s and != nulls, this is just the meat.
document.parentWindow.parent.reSize();

And this, which is in the parent html:

function reSize()
{
    try
    {
        var oBody   =   ifrm.document.body;
        var oFrame  =   document.all('ifrm');

        oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight) + 100;
    }
    //An error is raised if the IFrame domain != its container's domain
    catch(e) { }
}

However, calls to this are littered all over, in every place where resizing might have happened. Is there anywhere I can put this such that it is "automatic"? Some event I can trigger off of or something?

Relevant links:

  • How do I detect iframe resize?
  • resize iframe dynamically in ASP
  • how to detect iframe resize?
like image 676
Scott Stafford Avatar asked Nov 19 '25 18:11

Scott Stafford


1 Answers

Place this code right above the closing body tag of the parent window. place your iframe resizing code where commented.

OLD CODE:

<script>
    var intWinHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);

    var resizeIframe = function() {
        var newWinHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
        if(newWinHeight != intWinHeight) {
            intWinHeight = newWinHeight;
            // execute your Iframe resizing code here

        }
    }
    setInterval(resizeIframe, 100);
</script>

NEW CODE:

<script type="text/javascript">

    var iFrameId = document.getElementById('myIframe');
    var intWinHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
    var intIframHeight = (typeof iFrameId.contentWindow.window.innerHeight != 'undefined' ? iFrameId.contentWindow.window.innerHeight : iFrameId.contentWindow.document.body.offsetHeight);
    function exeResizeIframe() {
        var newWinHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
        var newIframHeight = (typeof iFrameId.contentWindow.window.innerHeight != 'undefined' ? iFrameId.contentWindow.window.innerHeight : iFrameId.contentWindow.document.body.offsetHeight);
        if((newWinHeight != intWinHeight) || intIframHeight != newIframHeight) {
            intWinHeight = newWinHeight;
            intIframHeight = newIframHeight;
            // execute your Iframe resizing code here

        }
    }
    setInterval("exeResizeIframe()", 100);
</script>

Please note, change the iframe ID due to security restrictions in IE < 9 only local paths will work in the ifram src

like image 65
Tim Wickstrom Avatar answered Nov 22 '25 09:11

Tim Wickstrom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!