Is there a cross-domain iframe height auto-resizer that works?

后端 未结 7 2419
暖寄归人
暖寄归人 2020-11-27 09:41

I tried a few solutions but wasn\'t successful. I\'m wondering if there is a solution out there preferably with an easy-to-follow tutorial.

7条回答
  •  猫巷女王i
    2020-11-27 10:17

    I have a script that drops in the iframe with it's content. It also makes sure that iFrameResizer exists (it injects it as a script) and then does the resizing.

    I'll drop in a simplified example below.

    // /js/embed-iframe-content.js
    
    (function(){
        // Note the id, we need to set this correctly on the script tag responsible for
        // requesting this file.
        var me = document.getElementById('my-iframe-content-loader-script-tag');
    
        function loadIFrame() {
            var ifrm = document.createElement('iframe');
            ifrm.id = 'my-iframe-identifier';
            ifrm.setAttribute('src', 'http://www.google.com');
            ifrm.style.width = '100%';
            ifrm.style.border = 0;
            // we initially hide the iframe to avoid seeing the iframe resizing
            ifrm.style.opacity = 0;
            ifrm.onload = function () {
                // this will resize our iframe
                iFrameResize({ log: true }, '#my-iframe-identifier');
                // make our iframe visible
                ifrm.style.opacity = 1;
            };
    
            me.insertAdjacentElement('afterend', ifrm);
        }
    
        if (!window.iFrameResize) {
            // We first need to ensure we inject the js required to resize our iframe.
    
            var resizerScriptTag = document.createElement('script');
            resizerScriptTag.type = 'text/javascript';
    
            // IMPORTANT: insert the script tag before attaching the onload and setting the src.
            me.insertAdjacentElement('afterend', ifrm);
    
            // IMPORTANT: attach the onload before setting the src.
            resizerScriptTag.onload = loadIFrame;
    
            // This a CDN resource to get the iFrameResizer code.
            // NOTE: You must have the below "coupled" script hosted by the content that
            // is loaded within the iframe:
            // https://unpkg.com/iframe-resizer@3.5.14/js/iframeResizer.contentWindow.min.js
            resizerScriptTag.src = 'https://unpkg.com/iframe-resizer@3.5.14/js/iframeResizer.min.js';
        } else {
            // Cool, the iFrameResizer exists so we can just load our iframe.
            loadIFrame();
        }    
    }())
    

    Then the iframe content can be injected anywhere within another page/site by using the script like so:

    
    

    The iframe content will be injected below wherever you place the script tag.

    Hope this is helpful to someone.

提交回复
热议问题