How do I use postMessage() in Javascript?

拥有回忆 提交于 2019-12-06 08:36:12

This is a two way implementation, meaning that the page you want to call needs to have a callback that listens to such a message and give an appropriate response. You can't simply use it as a swap replacement for AJAX. The best method for that is to use a server-side proxy.

See this page for an explanation of how postMessage works.

Yes, it is possible.

There is a nice demo of what exactly you want, here

document.getElementById("iframe").contentWindow.postMessage(
        document.getElementById("message").value,
        "http://anotherdomain.com"
);

handled on the second side with

window.onmessage = function(e){
  if ( e.origin !== "http://html5demos.com" ) {
    return;
  }

  document.getElementById("test").innerHTML = e.origin + " said: " + e.data;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!