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

后端 未结 5 408
粉色の甜心
粉色の甜心 2020-12-31 01:38

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 accordi

5条回答
  •  不知归路
    2020-12-31 01:57

    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.

提交回复
热议问题