Adam Heath
Adam Heath

Reputation: 4763

Determine if an iframe is visible on the screen

I have an iframe that is being loaded into external sites of which I cannot put any code in. What I would like to be able to determine from within my iframe (different domain) is if the iframe is currently within the 'viewport' of the browser (or if it is scrolled off the page).

Is there any way for this to be done? As I do not believe that I can access the scrolltop and viewport height properties of the parent page (due to cross domain), unless I am mistaken.

Thanks

Upvotes: 5

Views: 3186

Answers (2)

dekajoo
dekajoo

Reputation: 2112

I know this is an old question but some new API from Chrome have been released that could answer your question on the latest versions of Chrome (22+)

The InteractionObserver you can find it here: https://github.com/WICG/IntersectionObserver

There is a polyfill that is being developped (same repo) but it will not work from within the iframe afaik only if the script is on the parent webpage.

For other browsers their is some dark ways to do it based on timing attack, this article explains it pretty well: http://www.contextis.com/documents/2/Browser_Timing_Attacks.pdf

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82523

No, it can not be done because you can not access the document of the parent domain. That's needed to determine the position of the iframe element. You can get the size of the users screen, but that's about it. Your iframe could be invisible and you would have no way to tell. Sorry!

Upvotes: 4

Related Questions