hungpham hungpham - 1 year ago 144
Javascript Question

How to get height of iframe cross domain

I have iframe(cross domain) with src from facebook, twitter or etc. I need to get height of iframe but i got issue: "Permission denied to access property 'document'". Please help me to resolve this. Many thanks!

Answer Source

Couple issues. First, the height of the iframe is likely not what you want. I mean that's set explicitly in the HTML code of the page you control and is readily accessible and modifiable through any Javascript means. What it appears you are after is the height of the page inside the iframe. If that's the case, the simple answer is you can't, at least not with external services like Facebook/Twitter.

Due to security reasons, one can easily pass messages from child to parent, but not from parent to child, unless a communication pathway has been built into your javascript in both documents. There is a postMessage protocol for handling this in modern browsers. . But, it's wholly useless in this case unless the document you are communicating with is setup to handle an incoming postMessage, which to my knowledge Twitter/Facebook frequently are not.

If a parent document could freely communicate with children from different domains, then any javascript could effectively execute any series of commands on any site you're logged in as. The security implications of that are frightening and thankfully not possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download