Skip to content

Instantly share code, notes, and snippets.

@tiarno
Last active January 29, 2020 08:41
Show Gist options
  • Save tiarno/ec445e2e6c5141cbb893 to your computer and use it in GitHub Desktop.
Save tiarno/ec445e2e6c5141cbb893 to your computer and use it in GitHub Desktop.
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
Copy link
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