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


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


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

Related articles