Skip to content

Instantly share code, notes, and snippets.

@ojii
Last active September 3, 2016 05:11
Show Gist options
  • Save ojii/984a2307b8bef121a9fbd2764a8d6723 to your computer and use it in GitHub Desktop.
Save ojii/984a2307b8bef121a9fbd2764a8d6723 to your computer and use it in GitHub Desktop.
<pre id='hoge'></pre>
<input id='foobar' />
<button id='send'>send</button>
<script>
const hoge = document.getElementById('hoge');
const foobar = document.getElementById('foobar');
const send = document.getElementById('send');
let target;
if (window.opener) {
// 子window
target = window.opener;
} else {
// 親window
target = window.open(window.location, '_blank');
}
window.addEventListener('message', (message) => hoge.textContent += message.data + '\n');
send.addEventListener('click', () => {
target.postMessage(foobar.value, '*');
hoge.textContent += foobar.value + '\n';
foobar.value = '';
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment