Jump to content
  • 0

Iframe dynamic size


Didier

Question

Hi family,

 

 

i am currently using the following code to set the height and width of the iframe deployment manually:

 

<iframe name="Broker" title="Broker" src="https://c0dcv045.caspio.com/dp/aa4a60008d8bcb4785114729ba75" 
width=500 height=300>Sorry, but your browser does not support frames.</iframe>
 

This is as given on caspio's site so i just adjust it to my datapage...

 

However is very time consuming to have to pick and adjust the sizes everytime you deploy and given that the results will always grow as the system is used, the sizes must now be readjusted manually all the time.

 

how do i set it to always fit the screen automatically, so that the sizes grow with the growing results....

 

much appreciated in advance

 

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hello @Didier,

 

I was able to formulate a working solution by putting together workflows from external resources.

 

On the Footer of the DataPage you want to deploy as iframe, paste the snippet below (Make sure HTML Editor is disabled from the Advanced Tab).

 

//Script tested on Tabular Report
<script>

document.addEventListener('DataPageReady', function() {
  parent.postMessage(document.documentElement.scrollHeight, "*");
  console.log(document.documentElement.scrollHeight);
})

</script>

 

On cases of  Submission Form / Details Drilldown, you may want to put the snippet below on an HTML Block instead.

//Not tested
<script>
  parent.postMessage(document.documentElement.scrollHeight, "*");
  console.log(document.documentElement.scrollHeight);
</script>

 

Then, on the page where you will be embedding the iframe, place this code snippet:

Replace iframe src with the URL Deployment of your DataPage

<iframe id="ifr-cdr" src="https://xxx.caspio.com/dp/2abe231b344545ff9945230aaaeeffccc" frameborder="0">Sorry, but your browser does not support frames.</iframe>

<script>
// Create IE + others compatible event handler (Credits to: https://davidwalsh.name/window-iframe)
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

// Listen to message from child window
eventer(messageEvent,function(e) {
  document.getElementById('ifr-cdr').height = e.data;
},false);
</script>

 

Works for me.

Hope this helps.

 

-DN31337!

Link to comment
Share on other sites

  • 0
On 9/12/2019 at 11:14 AM, DefinitelyNot31337 said:

Hello @Didier,

 

I was able to formulate a working solution by putting together workflows from external resources.

 

On the Footer of the DataPage you want to deploy as iframe, paste the snippet below (Make sure HTML Editor is disabled from the Advanced Tab).

 


//Script tested on Tabular Report
<script>

document.addEventListener('DataPageReady', function() {
  parent.postMessage(document.documentElement.scrollHeight, "*");
  console.log(document.documentElement.scrollHeight);
})

</script>

 

On cases of  Submission Form / Details Drilldown, you may want to put the snippet below on an HTML Block instead.


//Not tested
<script>
  parent.postMessage(document.documentElement.scrollHeight, "*");
  console.log(document.documentElement.scrollHeight);
</script>

 

Then, on the page where you will be embedding the iframe, place this code snippet:

Replace iframe src with the URL Deployment of your DataPage


<iframe id="ifr-cdr" src="https://xxx.caspio.com/dp/2abe231b344545ff9945230aaaeeffccc" frameborder="0">Sorry, but your browser does not support frames.</iframe>

<script>
// Create IE + others compatible event handler (Credits to: https://davidwalsh.name/window-iframe)
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

// Listen to message from child window
eventer(messageEvent,function(e) {
  document.getElementById('ifr-cdr').height = e.data;
},false);
</script>

 

Works for me.

Hope this helps.

 

-DN31337!

@DefinitelyNot31337

This works great when you only have one iframe that you want to use this on.  I have 8 though and they all just use the last size that was sent to the console.  How can I use this for multiple iframes?

Link to comment
Share on other sites

  • 0

Hello @ChristopherNORD,

There are 2 ways to set the height for multiple iFrames, depending where you add them:

1) If you add a few DataPages with iFrames into another DataPage, then you can use '.contentWindow' because the the domain will be the same.

<script>
document.addEventListener('DataPageReady', function (event) {
 let frames = document.querySelectorAll("#caspioform iframe");
  for (let frame of frames) {
    frame.onload = function(){
    let frameHeight = frame.contentWindow.document.body.scrollHeight;
    frame.style.height = frameHeight + 16 + 'px';
    };
  }
});
</script>

2) In case you embed a few iFrames into your website, then we can't use '.contentWindow', but we can use '.postMessage' and provide mesages outside the frame with their names:

<!-- Add to the Footer of the DataPage 1 -->
<script>
document.addEventListener('DataPageReady', function() {
  let frame ={frame1 : document.documentElement.scrollHeight};
  parent.postMessage(frame, "*");
})  
</script>

<!-- Add to the Footer of the DataPage 2 -->
<script>
document.addEventListener('DataPageReady', function() {
  let frame ={frame2 : document.documentElement.scrollHeight};
  parent.postMessage(frame, "*");
})  
</script>

<!-- Add to the webpage where you wish to display 2 or more iFrames -->
<iframe id="frame1" name="" src="" title="" width="100%">Sorry, but your browser does not support frames.</iframe>
<iframe id="frame2" name="" src="" title="" width="100%">Sorry, but your browser does not support frames.</iframe>

<script>
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent,function(e) {
  document.querySelector("#frame1").style.height = e.data.frame1 + 'px';
  document.querySelector("#frame2").style.height = e.data.frame2 + 'px';
},false);  
</script>

Make sure that you have same IDs for your iFrames or change them in the code.

Hope it helps.

Link to comment
Share on other sites

  • 0

If an iframe is deployed within a List Report, is there a way to make each iframe adjust height automatically for each record in the List Report? For example, the List Report is the parent and is a list of active projects. The iframe child is a Tabular Report of tasks. Each project has one or more tasks filtered by priority, date, assigned to, etc. Without dynamic height, each iframe would be the same size. If we choose a size large enough to show all tasks for the largest project then we waste space for the projects with only one task. If we choose a more reasonable size then we miss tasks for the larger projects.

Thanks in advance!

Link to comment
Share on other sites

  • 0
On 12/20/2022 at 5:05 PM, Peterson said:

If an iframe is deployed within a List Report, is there a way to make each iframe adjust height automatically for each record in the List Report? For example, the List Report is the parent and is a list of active projects. The iframe child is a Tabular Report of tasks. Each project has one or more tasks filtered by priority, date, assigned to, etc. Without dynamic height, each iframe would be the same size. If we choose a size large enough to show all tasks for the largest project then we waste space for the projects with only one task. If we choose a more reasonable size then we miss tasks for the larger projects.

Thanks in advance!

Hi @Peterson, you may try the solution provided here: https://howto.caspio.com/tech-tips-and-articles/embedding-datapages-with-automatic-height-adjustment/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...