Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Messaging -- post message to the iFrame
<!DOCTYPE html>
<title>Post Message</title>
<form id="the-form" action="/">
<input type="text" id="my-message" value="Your message">
<input type="submit" value="postMessage">
<iframe id="my-iframe" src="postMessageTarget.html"></iframe>
window.onload = function () {
var iframeWin = document.getElementById("my-iframe").contentWindow,
form = document.getElementById("the-form"),
myMessage = document.getElementById("my-message");;
form.onsubmit = function () {
iframeWin.postMessage(myMessage.value, "/");
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment