window.postMessage not working JavaScript

Viewed 137

I have written a very basic code but it don't work. Please let me know my mistakes. Thank you.

Parent Side:

<!DOCTYPE html>
<html>
<head>
    <title>Parent</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function() {
        let el_win = window.open("https://www.example.com/child/", "_blank", "toolbar=no, scrollbars=no, resizable=no, top=0, left=0, width="+screen.availWidth+", height="+screen.availHeight);
        el_win.focus();
        el_win.postMessage("hello","*");

    }
</script>
</body>
</html>

Child Side:

<!DOCTYPE html>
<html>
<head>
    <title>child</title>
</head>
<body>
<script type="text/javascript">
    
window.addEventListener("message", function(e) {
    alert("Message Recieved");
    console.log(e.data);
}, false);
</script>
</body>
</html>

Even alert() is not called in child window and also no data in console.log()

0 Answers
Related