Created
July 30, 2020 18:15
-
-
Save m3g4p0p/4317b90b4d90d67a6b7bdf440207ca3c to your computer and use it in GitHub Desktop.
Monitor events inside iframe
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Inner</title> | |
</head> | |
<body> | |
<a href="#" id="my-link">Click me</a> | |
</body> | |
</html> |
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
const message = document.getElementById('message') | |
const iframe = document.getElementById('my-iframe') | |
function displayEvent (event) { | |
message.textContent = event.type | |
} | |
iframe.contentWindow.addEventListener('load', () => { | |
const link = iframe.contentDocument.getElementById('my-link') | |
link.addEventListener('mouseover', displayEvent) | |
link.addEventListener('mouseout', displayEvent) | |
link.addEventListener('click', displayEvent) | |
}) |
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 lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Outer</title> | |
</head> | |
<body> | |
<p id="message"> </p> | |
<iframe src="inner.html" id="my-iframe"></iframe> | |
<script src="main.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment