{"id":76,"date":"2019-05-02T15:12:54","date_gmt":"2019-05-02T15:12:54","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=76"},"modified":"2019-05-02T15:39:23","modified_gmt":"2019-05-02T15:39:23","slug":"javascript-communications-between-window-iframe-using-postmessage","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/","title":{"rendered":"Javascript: Communications between Window &#038; Iframe Using postMessage()"},"content":{"rendered":"<p>From Iframe to Window:<\/p>\n<pre>\/\/The \"*\" can be a domain name to enhance the security\r\n\/\/The receiver can check the incoming message before executing any funcions.\r\nparent.postMessage('SomeText', '*'); \r\n<\/pre>\n<p>From Window to Iframe:<\/p>\n<pre>var frame = document.getElementById('frame'); \r\nframe.contentWindow.postMessage('SomeText', '*');\r\n<\/pre>\n<p>Add Listening for the message events:<\/p>\n<pre>window.addEventListener('message', function(event) { \r\n      console.log(event.data); \r\n}); \r\n<\/pre>\n<p>Useful Resources:<br \/>\n<a href=\"https:\/\/davidwalsh.name\/window-iframe\">https:\/\/davidwalsh.name\/window-iframe<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/postMessage\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/postMessage<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From Iframe to Window: \/\/The &#8220;*&#8221; can be a domain name to enhance the security \/\/The receiver can check the incoming message before executing any funcions. parent.postMessage(&#8216;SomeText&#8217;, &#8216;*&#8217;); From Window to Iframe: var frame = document.getElementById(&#8216;frame&#8217;); frame.contentWindow.postMessage(&#8216;SomeText&#8217;, &#8216;*&#8217;); Add Listening for the message events: window.addEventListener(&#8216;message&#8217;, function(event) { console.log(event.data); }); Useful Resources: https:\/\/davidwalsh.name\/window-iframe https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/postMessage<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-4"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Javascript: Communications between Window &amp; Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Javascript: Communications between Window &amp; Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"From Iframe to Window: \/\/The &quot;*&quot; can be a domain name to enhance the security \/\/The receiver can check the incoming message before executing any funcions. parent.postMessage(&#039;SomeText&#039;, &#039;*&#039;); From Window to Iframe: var frame = document.getElementById(&#039;frame&#039;); frame.contentWindow.postMessage(&#039;SomeText&#039;, &#039;*&#039;); Add Listening for the message events: window.addEventListener(&#039;message&#039;, function(event) { console.log(event.data); }); Useful Resources: https:\/\/davidwalsh.name\/window-iframe https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/postMessage\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-02T15:12:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-02T15:39:23+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/\",\"name\":\"Javascript: Communications between Window & Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-05-02T15:12:54+00:00\",\"dateModified\":\"2019-05-02T15:39:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Javascript: Communications between Window &#038; Iframe Using postMessage()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Javascript: Communications between Window & Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/","og_locale":"en_US","og_type":"article","og_title":"Javascript: Communications between Window & Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"From Iframe to Window: \/\/The \"*\" can be a domain name to enhance the security \/\/The receiver can check the incoming message before executing any funcions. parent.postMessage('SomeText', '*'); From Window to Iframe: var frame = document.getElementById('frame'); frame.contentWindow.postMessage('SomeText', '*'); Add Listening for the message events: window.addEventListener('message', function(event) { console.log(event.data); }); Useful Resources: https:\/\/davidwalsh.name\/window-iframe https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/postMessage","og_url":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-05-02T15:12:54+00:00","article_modified_time":"2019-05-02T15:39:23+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/","url":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/","name":"Javascript: Communications between Window & Iframe Using postMessage() - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-05-02T15:12:54+00:00","dateModified":"2019-05-02T15:39:23+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/javascript-communications-between-window-iframe-using-postmessage\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Javascript: Communications between Window &#038; Iframe Using postMessage()"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":3,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}