How to get real iframe body height?

Member

by dedrick , in category: Javascript , 3 months ago

How to get real iframe body height?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 3 months ago

@dedrick 

To get the real iframe body height, you can follow these steps:

  1. Access the iframe element using JavaScript by selecting it with document.getElementById() or any other selector method.
  2. Use the contentDocument property of the iframe element to access the document inside the iframe.
  3. Get the body element of the iframe document using the contentDocument.body property.
  4. Finally, retrieve the height of the body element using the offsetHeight property.


Here's an example of how you can implement this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Get the iframe element
var iframe = document.getElementById('your-iframe-id');

// Get the document inside the iframe
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

// Get the body element inside the iframe document
var iframeBody = iframeDocument.body;

// Get the real height of the body element
var bodyHeight = iframeBody.offsetHeight;


Replace 'your-iframe-id' with the actual ID of your iframe element. The bodyHeight variable will then contain the real height of the iframe body.