Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the height of an iframe with javascript from inside the iframe? What about pages with multiple iframes?

Tags:

Is there a way to detect the height and width of an iframe, by executing a script from inside the iframe? I need to dynamically position some elements in the iframe according to the different height/width of the iframe.

Would there be any difference if there are multiple iframes in the same page? i.e. each iframe wants to find its own dimensions.

Javascript or jquery solutions welcomed.

Thanks!

<iframe src='http://example.com' width='640' height='480' scrolling='no' frameborder='0' longdesc='http://example.com'></iframe> 

The iframes have to be embeded on other sites, and as mentioned by one of the answers below, I've hit permission problems.

like image 580
VKen Avatar asked Feb 23 '11 05:02

VKen


People also ask

How do I adjust the iframe height automatically?

Answer: Use the contentWindow Property You can use the JavaScript contentWindow property to make an iFrame automatically adjust its height according to the contents inside it, so that no vertical scrollbar will appear.

What is iframe height?

The standard size of an iframe for desktop is a width of "560" and height of "315."

How do I make my iframe height 100%?

given the iframe is directly under body. If the iframe has a parent between itself and the body, the iframe will still get the height of its parent. One must explicitly set the height of every parent to 100% as well (if that's what one wants).


2 Answers

If your iframe’s page and its parent page are served from different domains (meaning you can’t access the parent page’s DOM properties from the iframe page), then I think it’s the same as when you’re trying to work out the viewport height.

For that, see:

  • Get the browser viewport dimensions with JavaScript

Or possibly this:

  • http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
like image 147
Paul D. Waite Avatar answered Oct 12 '22 23:10

Paul D. Waite


Be aware that code like this:

var thisIframesHeight = window.parent.$("iframe.itsID").height(); 

will only be safe if the source of the iframe and parent window are from the same domain. If not you will get permission denied problems and you will have to take another approach.

like image 33
Julian Jelfs Avatar answered Oct 12 '22 22:10

Julian Jelfs