Skip to content

Instantly share code, notes, and snippets.

@tiarno tiarno/iframe_vis.js
Last active Jun 18, 2019

Embed
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;
}
};
@tiarno

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.