最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Window.postMessage() issue - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jan 12, 2020 at 9:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 14, 2015 at 15:10 Roman NewazaRoman Newaza 11.7k12 gold badges63 silver badges94 bronze badges 4
  • are you receiving any errors in the console? – David Commented Aug 14, 2015 at 15:11
  • 2 no, no errors at all – Roman Newaza Commented Aug 14, 2015 at 15:16
  • are the files in different relative paths on the different servers – David Commented Aug 14, 2015 at 15:19
  • on local: popup.html, remote: foo/index.html – Roman Newaza Commented Aug 14, 2015 at 15:20
Add a ment  | 

2 Answers 2

Reset to default 4

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 es 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 bination 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>

The eventListener should be attached to window instead of popup:

window.addEventListener(
    'message',
    function (event) {
        console.log(event.data);
    },
    false
);

In your child window (popup), you are posting a message to the parent window, but the eventListener is attached to the child window instead.

发布评论

评论列表(0)

  1. 暂无评论