How can I get a parent window's height from wi

2019-01-23 05:17发布

问题:

Within an iFrame, we need to get the parent's window/document height.

Is there a way to get this using jQuery?

I know we can use $(document).height() to get a page's height. I just can't figure out how to get the parent's value from within the iFrame.

Please help!

回答1:

jQuery is not needed.

parent.document.body.clientHeight

That works for me with IE7 and FF3.



回答2:

I tried the clientHeight approach on a website where both Iframes where on the same domain and it didn't work. (return 0).

After a lot of testing the best way I have found (and I'll be happy to learn a better way) is to create a function on the parent which returns the document height, something like:

Parent:

function getDocumentHeight(){
   return $(document).height();
}

Iframe:

var parentDocHeight = parent.getDocumentHeight();


回答3:

In jQuery you can do

$(parent.window).width();


回答4:

Another possibility is :

$(window.parent.document).height()