var iframe = document.getElementById('myIFrame');
iframe.contentWindow.body.addEventListener('mouseup', Handler);
function Handler() {
alert('works');
}
var iframe = document.getElementsByTagName('iframe')[0],
iDoc = iframe.contentWindow // sometimes glamorous naming of variable
|| iframe.contentDocument; // makes your code working :)
if (iDoc.document) {
iDoc = iDoc.document;
iDoc.body.addEventListener('afterLayout', function(){
console.log('works');
});
};
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<h1>CHILD</h1>
<div>
<h3>Received data:</h3>
<div id="placeholder"></div>
</div>
<script>
/* DEFINITIONS */
var parentWindow = window.parent;
/*
* RECEIVE MESSAGE FROM PARENT
*/
window.addEventListener("message", (e) => {
var data = e.data;
console.log("RECEIVED message from PARENT TO CHILD", data);
document.getElementById("placeholder").innerText = data;
});
/* SHAKE HAND WITH PARENT */
window.addEventListener("load", () => {
parentWindow.postMessage("shakehand", "*");
});
</script>
</body>
</html>