0

I'm new to JavaScript and am having issues using the iframe-resizer library.

I have an Angular component HTML file for my iFrame which contains the code below. As you can see, I'm including the iframeResizer.min.js file, and I'm building the content of my iFrame on the fly using srcdoc (I include the iframeResizer.contentWindow.min.js file inside this framed content):

<script type='text/javascript' src='../iframeResizer.min.js'></script>

<iframe id="myIframe" srcdoc="<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?<p>On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.</p><script type='text/javascript' src='../iframeResizer.contentWindow.min.js'></script>" scrolling="no"></iframe>

<script>iFrameResize({log:true}, '#myIframe')</script>

I also have an scss file for my component which contains the following:

iframe {
    width: 1px;
    min-width: 100%;
}

I'm not seeing my iFrame being resized, nor am I seeing any iframe-resizer logs in the console. Nothing is happening at all. Any idea why this might be happening? What am I doing wrong? Do I need an Angular directive to expose necessary variables and behavior -- if so, do you have an example of that? Sorry for the noob questions and thanks in advance for your help.

keharris
  • 49
  • 1
  • 1
  • 9
  • I'm looking at the readme on github and he calls the iFrameResize function in a script block, not the onload of the iframe – chiliNUT Mar 29 '18 at 15:20
  • Hi chiliNUT, thanks for your reply. I've moved the call to the iFrameResize function to a script block instead. Now, the "iFrameResize is not defined" issue is gone but I'm still not seeing the iFrame being resized, nor am I seeing logs. Any ideas? – keharris Mar 29 '18 at 15:24
  • Seems like it could be a compatibility issue with Angular? I wasn't seeing logs when I had the code in my component, but when I put it in my index file, the logs started showing – keharris Mar 29 '18 at 16:24

1 Answers1

1

FWIW, when I had to do this same thing in angular, I used the method here to create a directive to apply to iframes to add a load handler:

https://stackoverflow.com/a/23948165/2079345

and then in the load handler I did

        let $frame=$('#the-iframe');
        setInterval(function () {
            let height = parseInt($frame.contents().find('body').height());
            if (height !== oldHeight) {
                $frame.height(height + 30);
                oldHeight = height;
            }
        })

The plus 30 was for padding issues, your use case may differ

chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • Thanks chiliNUT -- unfortunately I'm using Angular 4. That looks like AngularJS :( – keharris Mar 29 '18 at 16:37
  • I think you're on the right track though. It looks like we need to expose some variables and behavior so that they can be used by Angular. Does anyone have an example of this? – keharris Mar 29 '18 at 17:39
  • @keharris I think you can just skip the onload logic altogether and keep the `setInterval` part, it would still work – chiliNUT Mar 29 '18 at 18:19
  • would I use this in conjunction with the iframe-resizer library? Does this work for cross-domain iFrames? – keharris Mar 29 '18 at 19:01
  • you would use it instead, except that it does not handle cross domain. since that library doesnt seem to be angular specific, i wonder if it would solve all of your problems to just to `$(document).ready(function(){iFrameResize({log:true}, '#myIframe');});` – chiliNUT Mar 29 '18 at 20:24