Skip to content

Instantly share code, notes, and snippets.

Last active Jan 29, 2020
What would you like to do?
javascript: is element inside an iframe visible within the viewport?
// Given an iframe id and an anchor id that is present within the iframe,
// determine whether the element is visible/present inside the window viewport.
// This is not about the css 'display' property; this shows whether
// the window viewport contains the element.
var isVisible = function (anchor, iframe_id) {
var ifrId = iframe_id || 'bv_page';
var ifrOffset = window.parent.document.getElementById(ifrId).offsetTop;
var myloc = document.getElementById(anchor).offsetTop + ifrOffset;
var viewtop = window.parent.scrollY;
var viewbot = viewtop + window.parent.innerHeight;
if (viewbot > myloc && myloc > viewtop){
return true;
} else {
return false;
Copy link

tiarno commented Jul 27, 2015

Useful for testing internal links on a webpage with iframes, especially when links are intercepted by javascript out of your control. This script can be used to show whether the link target is in view after the link is clicked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment