Last active
September 3, 2016 05:11
-
-
Save ojii/984a2307b8bef121a9fbd2764a8d6723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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