//top page
<script type="text/javascript"> if (typeof window.postMessage === undefined) { alert("not support"); } function main(e) { alert("where are you from ?"+e.constructor); } window.addEventListener("message", main, true); </script> <iframe src="HTMLPage1.htm" id="widget"></iframe> <script type="text/javascript"> function postms() { document.getElementsByTagName("iframe")[0].contentWindow.postMessage("hello world", "http://localhost:9273"); } </script>
iframe page:
<script type="text/javascript"> function messageHandler(e) { alert(e.origin + " come from"); window.top.postMessage("send back", e.origin); } window.addEventListener("message", messageHandler, true); </script>