Created
November 19, 2015 15:17
-
-
Save 4e4c52/5e6632688f3a9bf1d056 to your computer and use it in GitHub Desktop.
Iframe aware of its visibility.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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