{"id":1402,"date":"2014-08-19T15:49:09","date_gmt":"2014-08-19T13:49:09","guid":{"rendered":"http:\/\/blog.kodono.info\/wordpress\/?p=1402"},"modified":"2014-08-19T15:49:09","modified_gmt":"2014-08-19T13:49:09","slug":"communication-between-iframe-and-parent-window-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2014\/08\/19\/communication-between-iframe-and-parent-window-javascript\/","title":{"rendered":"Communication between iframe and parent window [JavaScript]"},"content":{"rendered":"<p>In the iframe you&#8217;ll use the below code:<\/p>\n<pre class=\"brush:javascript\">\r\nwindow.parent.postMessage(\"Hello World !\", \"http:\/\/your.site.web\");\r\n<\/pre>\n<p>And in the parent window:<\/p>\n<pre class=\"brush:javascript\">\r\n\/\/ function that will be called when the message is received\r\nfunction receiveMessage(event) {\r\n  alert(event.data); \/\/ it'll show \"Hello World !\"\r\n}\r\n\/\/ we listen to a message\r\nif (window.addEventListener) {\r\n  window.addEventListener('message', receiveMessage, false);\r\n} else if (window.attachEvent) { \/\/ IE8\r\n  window.attachEvent('onmessage', receiveMessage);\r\n}\r\n<\/pre>\n<p>It&#8217;s working from IE8+, and all modern browsers, but make sure you have a HTML5 doctype (<em>&lt;!DOCTYPE html&gt;<\/em>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the iframe you&#8217;ll use the below code: window.parent.postMessage(&#8220;Hello World !&#8221;, &#8220;http:\/\/your.site.web&#8221;); And in the parent window: \/\/ function that will be called when the message is received function receiveMessage(event) { alert(event.data); \/\/ it&#8217;ll show &#8220;Hello World !&#8221; } \/\/ we listen to a message if (window.addEventListener) { window.addEventListener(&#8216;message&#8217;, receiveMessage, false); } else if (window.attachEvent) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[15,11,33],"tags":[24,151,158],"class_list":["post-1402","post","type-post","status-publish","format-standard","hentry","category-astuce","category-niveau-debutant","category-programmation","tag-javascript","tag-niveau-debutant","tag-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1402"}],"version-history":[{"count":3,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1402\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}