Home>

At a certain site
I found the following code

parent.postMessage ({name: window.name}, window.location.origin);

Looking at the grammar of .postMessage (),
"OtherWindow.postMessage (message, targetOrigin, [transfer]);"
Was written.

Does postMessage () actually have a name?

  • Answer # 1

    Does postMessage () actually have a name?

    You can put it in.messageYou can pass an object of any shape (if the sender and receiver can handle it and serialize it).

  • Answer # 2

    //me.html

    <script>
    window.addEventListener ('DOMContentLoaded', () =>{
      document.querySelector ('# btn'). addEventListener ('click', () =>{
        var win = document.querySelector ('# hoge'). contentWindow;
        win.postMessage ({name: "hoge"});
      });
    });
    </script>
    <input type = "button" value = "change" id = "btn">


    //you.html

    <script>
    window.addEventListener ('message', (e) =>{
      document.querySelector ('# fuga'). textContent = e.data.name;
    });
    </script>
     fuga

Related articles