Skip to content

Instantly share code, notes, and snippets.

@4e4c52
Created November 19, 2015 15:17
Show Gist options
  • Save 4e4c52/5e6632688f3a9bf1d056 to your computer and use it in GitHub Desktop.
Save 4e4c52/5e6632688f3a9bf1d056 to your computer and use it in GitHub Desktop.
Iframe aware of its visibility.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Iframe</title>
</head>
<body>
<h2>Iframe Content</h2>
<script>
// select the iframe node is the parent window
var target = window.parent.document.querySelector("#iframe");
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type == "attributes") {
if (mutation.target.style.display === "block") {
alert("Iframe shown");
}
else {
alert("Iframe hidden");
}
}
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
// observer.disconnect();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment