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

78,492

Solution 1

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:

Or possibly this:

Solution 2

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.

Solution 3

Each <iframe> would need an id I suppose. And then inside the <iframe> you would reference it like this:

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

Solution 4

may be a bit late, as all the other answers are older :-) but... here´s my solution. Tested in actual FF, Chrome and Safari 5.0.

css:

iframe {border:0; overflow:hidden;}

javascript:

$(document).ready(function(){
    $("iframe").load( function () {
        var c = (this.contentWindow || this.contentDocument);
        if (c.document) d = c.document;
        $(this).css({
            height: $(d).outerHeight(),
            width: $(d).outerWidth()
        });
    });
});

Hope this will help anybody.

Share:
78,492

Related videos on Youtube

VKen
Author by

VKen

More than 10 years in software engineering, mostly programs with Javascript, Python, and Php. Dabbled a little in C, C++, Qbasic, Visual Basic, Java, Ruby, Erlang, Lua, ActionScript3, Scheme (LISP), (Bash/Zsh) shell scripts, ELM, and Go (Golang). A VIM user, who also dabbled in the dark side by exploring CLISP in EMACS. Does web development with HTML5 and CSS3, and JavaScript (ES6/2015+) &amp; server-side JS (node.js). Frameworks, oh frameworks, more and more of them everyday. For frontend UI in web browser, there are javascript and compiled-to-javascript languages. Plays with Node.js (io.js a short while), jQuery, Backbone, Angular 1.x, grunt, yeoman, kendoUI, dojokit, and toying with ELM (identity-crisis) and React + Redux (identity-crisis). Explores the javascript/node.js world with NPM. Takes care of the dependency mess with RequireJS, and Webpack. For Python (CPython), plays with Django, Scrapy, Pandas, NLTK. Explores the Python world with Python-pip and the awesome batteries-included libraries/packages in PyPI and various git repository platforms. For Php, plays with Laravel, CodeIgniter. Rarely touches Symphony, but more of its components. Explores the Php world with Composer. DevOps is cool. Automation with python-fabric, Ansible, puppet. Containerization with Openshift, Kubernetes, Docker, Helm. Big data scaling with dataflow design. Exploring data-intensive application design with Apache Kafka and RabbitMQ. Exploring the world of artificial intelligence via machine learning.

Updated on July 09, 2022

Comments

  • VKen
    VKen almost 2 years

    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.

  • VKen
    VKen about 13 years
    Hi, I think this answer is good. Do you also know of any other ways which does not need ID-ing?
  • Anriëtte Myburgh
    Anriëtte Myburgh about 13 years
    No I don't think so, not if you're working from INSIDE the <iframe>. If you're working from the parent document, then yes, there are a few other ways.
  • VKen
    VKen about 13 years
    Yes, I'm hitting permission problems as the iframes are embeded in other sites. Is there any other way?
  • VKen
    VKen about 13 years
    Thanks, this gives the dimensions property of the iframe. Exactly as I needed it.
  • Anuj Pandey
    Anuj Pandey almost 11 years
    window.innerWidth & window.innerHeight should be a good option too.
  • Robert Koritnik
    Robert Koritnik over 7 years
    @VKen: This is long past overdue, but yes there is window.postMessage that can be used to communicate between inner and outer script scope in cases when they have different origins.
  • VKen
    VKen over 7 years
    @RobertKoritnik: Thanks! I learned something new today after all these years! caniuse.com/#feat=x-doc-messaging and developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
  • Sean
    Sean about 4 years
    if not c.document, d is undefined