How to get the height of a iframe?

by elise_daugherty , in category: Javascript , 5 days ago

How to get the height of a iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 4 days ago

@elise_daugherty 

You can get the height of an iframe using JavaScript by accessing the contentWindow and scrollHeight properties of the iframe element.


Here is an example code snippet to get the height of an iframe:

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

// Get the content window of the iframe
var iframeWindow = iframe.contentWindow;

// Get the scroll height of the iframe content
var iframeHeight = iframeWindow.document.body.scrollHeight;

// Log the height of the iframe
console.log("Height of the iframe: ", iframeHeight);


Replace 'your-iframe-id' with the id of your iframe element in the above code. This code will log the height of the iframe content in the console.