5

I have an iframe that has a variable height which is not known in advance.

Currently, if the section in which the iframe loads is too small, the iframe loads with internal scroll bars. If the iframe happens to be a shorter iframe, there is empty space below the iframe before the footer begins.

Is there a solution available to this type of problem?

zgall1
  • 2,865
  • 5
  • 23
  • 39
  • I checked in Chrome and I don't see an issues – Tarun Lalwani Jun 27 '19 at 07:33
  • Empty space - https://www.evernote.com/l/AYu2MyDFU6RDNYJCTVdtAPOFAzyn4gnQvfU Internal scrollbars - https://www.evernote.com/l/AYtqRRgK6KlDiLXrc7a31k3TpstElKKvsRw – zgall1 Jun 27 '19 at 12:17
  • there might be a way using javascript. but the first thing i notice is the content is from another domain. can you change that or can you include the cross origin allow header in the response from the iframes source? – relief.melone Jul 03 '19 at 06:18
  • I can't get any of these answers to work unfortunately. It is possible the answer from @claytronicon allows you to resize the iframe without having access to the iframe origin server but that I just don't know what I am doing. – zgall1 Jul 04 '19 at 12:58

4 Answers4

4

Well, adding simple JS code to get the iframe content height and setting the container height will do, as suggested by @relief.melone.

Other simple solution that can be of help, as an alternative : https://github.com/davidjbradshaw/iframe-resizer

Furqan Rahamath
  • 2,034
  • 1
  • 19
  • 29
  • I have tried the iframe resizer code but I am still getting a cross origin access error. Any chance you could take a look at the site and see what I might be doing wrong? – zgall1 Jul 03 '19 at 16:21
  • 1
    https://github.com/davidjbradshaw/iframe-resizer/blob/master/docs/troubleshooting.md#iframe-not-resizing Do you have control over the iframe document you are fetching? – Furqan Rahamath Jul 03 '19 at 20:29
  • No I don’t have control – zgall1 Jul 03 '19 at 22:17
  • @zgall1 It appears that if you don't have control over the incoming iframe content, you won't be able to manipulate the height using the above script. – Furqan Rahamath Jul 03 '19 at 22:40
2

In reference to my comment. The first thing you have to solve is your cross origins problem. Most browsers will block requests to other websites if the response does not include the current host in their cross origins allow header. So in your case the header from your request to the iframe contents needs to include the header

Access-Control-Allow-Origin: http://159.89.229.184

and

Access-Control-Allow-Mehtods: GET

Also see https://de.wikipedia.org/wiki/Cross-Origin_Resource_Sharing for more info on this.


Now to the actual solution.

You need to determine the height of your iframes contents and then set the height accordingly. You can do this by adding a javascript function. In your head section add

 <script>
     const setHeight = (frame) => {
       frame.style.height = `${frame.contentWindow.document.body.scrollHeight}px`
     }
 </script>

and your iframe needs to include the onload event

<iframe ... scrolling="no" onload="setHeight(this)" />

This should solve your problem. But as I mentioned just if you allow the cross origin access. Otherwise you access to the document of frame.contentWindow will get rejected with an error like

VM747:1 Uncaught DOMException: Blocked a frame with origin "http://159.89.229.184" from accessing a cross-origin frame.

I also made an example on glitch to demonstrate how it works (Click on Show to see it in action)

https://glitch.com/edit/#!/iframe-varialbe-height

relief.melone
  • 3,042
  • 1
  • 28
  • 57
  • I have tried this code but I am still getting a cross origin access error. Any chance you could take a look at the site and see what I might be doing wrong? – zgall1 Jul 03 '19 at 16:20
  • Do you have access to both sites? the cross origin header can only be changed if you control the webserver. Depending on what webserver you use the header must be set changing its configuration. What webserver are you using? Or even easier. Is it possible you host both htmls on the same server? I might be able to help you on that but you should open another question for this, as it's not really related to this topic – relief.melone Jul 03 '19 at 18:24
  • would be great if you could accept the answer to this problem though before the bounty expires :D just post the link to the question regarding your cors problem and ill try to help you out there – relief.melone Jul 03 '19 at 18:33
  • I only control one web server. It is very doubtful that I could get the owner of the other server to make a change to their code. – zgall1 Jul 03 '19 at 22:16
  • which one do you control? – relief.melone Jul 04 '19 at 03:42
  • anyways. now that I am fully awake. Without that Header in the Servers response it's unlikely that you get access to the iframe and therefor its properties such as height . This is a security concept used by all modern browsers i can think off. Probably an old IE8 or something will allow it, but that's about it – relief.melone Jul 04 '19 at 06:05
2

I had s situation where the height of the iFrame content changed dynamically, and I told the parent frame (containing the iFrame) to change it's height accordingly using postMessage: like this

Parent window:

 <section class="module">
                <div class="row">
                    <!-- Content column start -->
                    <div class="col-sm-12" id="web-version-container">
                         <iframe id="web-version-frame" src="index.html" allowfullscreen=false frameborder="0" style="width:100%; height:100%;min-height:800px;">
                        </iframe>
                    </div>
                </div> <!-- .row -->    
        </section>

        <script>            
        window.addEventListener('message', function(event) { 
            // IMPORTANT: Check the origin of the data! 
            if (~event.origin.indexOf('https://yourdomain.com')) { 
                // The data has been sent from your site 

                if (event.data.messageName) {
                        switch (event.data.messageName) {
                            case "documentHeight":
                            if (event.data.messageValue && parseInt(event.data.messageValue) > 500);
                            var newHeight = parseInt(event.data.messageValue) + 50;
                            $("#web-version-frame").css("height",newHeight.toString() + "px"); 
                            break;
                    }
                }
                // The data sent with postMessage is stored in event.data 
            } else { 
                // The data hasn't been sent from your site! 
                // Be careful! Do not use it. 
              return; 
            } 
            }); 

        </script>

Child window:

if (window.parent) {
          window.parent.postMessage(
            {
              messageName: "documentHeight",
              messageValue: $(".content-active").height()
            },
            "*"
          );
        }

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Claytronicon
  • 1,437
  • 13
  • 14
0

In the iframe that you have added in second example has the css property min-height as 1600px. Use a percentage for min-height or height to fix the issue. Added min-height: 275vh; and it fixed the issue.

.job-detail-iframe iframe{
    width: 100%;
    min-height: 275vh;
}

Check this out also as a reference.

Harshana
  • 524
  • 3
  • 16