Search code examples
javascriptdom-eventspostmessage

Window.postMessage() issue


I cannot get any data if I use different domains. If I run it on the same server, no problem with it - message is obtained.

index.html:

<head>
    <title>Test 1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
        function popupResize() {
            var popup = window.open('popup.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
            // var popup = window.open('https://foo/index.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
            window.addEventListener(
                'message',
                function(event) {
                    console.log(event.data);
                },
                false
            );
        }
    </script>
</head>
<body>
    <a href='javascript:void(0)' onClick="popupResize(); return false;">Go!</a>
</body>

popup.html:

    <head>
    <title>Game history details</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script>
        function postSize() {
            var h = 100;
            var w = 200;
            opener.postMessage([h, w], '*');
        }
    </script>
</head>
<body onload="postSize();">
    test 1
</body>

How to get it working using different servers?


Solution

  • Problem 1

    popup.addEventListener(
    

    You need to listen for the event on your original window, not on the popup. The popup is where the message comes from, not where it is sent.

    Use window.addEventListener( or just addEventListener(.

    Problem 2

    {h, w}
    

    ES6 shorthand property names are not supported by IE, Opera, Safari or Android Mobile. They are best avoided.

    Problem 3

    parent.postMessage({h, w}, '*');
    

    You are sending a message to the opening window, not the parent frame. There is no parent frame (so parent recurses onto window).

    That should be:

     opener.postMessage({h: h, w: w}, '*');
    

    Problem 4

    <script type="text/javascript">
       var popup = window.open('popup.html', '', 'resizable=yes,scrollbars=yes,width=500,height=500');
    

    Your script does not have permission to open a new window except in response to a user event. That code needs moving into a function and then called from, for instance, a click event.


    If I run it on the same server, no problem with it - message is obtained.

    It is the combination of problems 1 and 3 that cause this. You are binding an event handler to the popup (which you can only do from the opening window if it is on the same origin) and you are posting a message from the popup to itself (because parent === window).


    Complete, albeit not best practise, code that works in my tests:

    http://localhost:7007/index.html

    <!DOCTYPE html>
    <html>
    <script>
    addEventListener("message", function (event) {
        document.body.appendChild(document.createTextNode(event.data));
    });
    function pop() {
        window.open("http://127.0.0.1:7007/popup.html");
    }
    </script>
    <input type="button" onclick="pop()">
    

    http://127.0.0.1:7007/popup.html

    <!DOCTYPE html>
    <html>
    <script>
    opener.postMessage([123, 456], '*');
    </script>
    <h1>popup</h1>