Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't access source object when window.postMessage in Edge

I am currently using Edge 44.18362, I have an authentication window, and a parent window which opens it, once the authentication is done I am posting the message to the parent (opener window) like this:

$window.opener.postMessage(data,URL);

and I am listening to the event in the parent window like this:

window.addEventListener('message', someFunc, false);

and someFunc() is defined like this:

function someFunc(windowData){
//here I am accessing source as windowData.source
}

I am trying to access the source attribute of MessageEvent to access the URL and stuff. In Chrome/Firefox I am able to access source object and functionality works fine, but when it comes to Edge I am not able to access source object and I see this error in console.
!0:
enter image description here
What is going wrong here, what can be done to fix this?

Added try-catch around the block of code, this is the error I see. enter image description here

like image 860
raviraja Avatar asked Oct 16 '22 05:10

raviraja


1 Answers

I made a test with the following code sample and it can work well in Edge:

Page1.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script>
        function openChild() {
            var childwin = window.open('Page2.html', 'popup', 'height=300px, width=500px');
        }

        window.addEventListener("message", (event) => {
            let txt = document.querySelector('#txtMsg');
            txt.value = `Name is ${event.data.pName} Age is  ${event.data.pAge}`;
            console.log(event.source);  //update: get event.source
        });
    </script>
</head>
<body>
    <form>
        <fieldset>
            <input type='button' id='btnopen' value='Open child' onclick='openChild();' />
            <input type='text' id='txtMsg' />
        </fieldset>
    </form>
</body>
</html>

Page2.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script>
        function sendMessage() {
            let msg = { pName: "Bob", pAge: "35" };
            window.opener.postMessage(msg, 'http://domain_name/Page1.html')
            window.opener.focus();
        }
    </script>
</head>
<body>
    <form>
        <fieldset>            
            <input type='button' id='btnSendMsg' value='Send Message' onclick='sendMessage();' />
        </fieldset>
    </form>
</body>
</html>

Result:

enter image description here

You could refer to the sample, if still not working, please provide a minimal sample which can reproduce the issue. With only the above code you provide, I can't reproduce the issue.

like image 182
Yu Zhou Avatar answered Oct 18 '22 15:10

Yu Zhou