Created
March 28, 2018 10:01
-
-
Save rkaw92/f53d7714e11b804e2fc5b690f41bbe62 to your computer and use it in GitHub Desktop.
Cross-window data passing in JS
This file contains 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
<!DOCTYPE html PUBLIC> | |
<html> | |
<head> | |
<title>Root window</title> | |
</head> | |
<body> | |
<p id="greeter">Nothing here yet...</p> | |
<button id="opener">Open a new window</button> | |
<script> | |
window.greet = function greet() { | |
document.getElementById('greeter').textContent = 'Hello, world!'; | |
}; | |
function openAnother() { | |
const myWindow = window.open('about:blank', 'My New Window'); | |
myWindow.document.write('<button id="delegator">Greet in root!</button>'); | |
console.log(myWindow.document); | |
myWindow.document.getElementById('delegator').addEventListener('click', window.greet); | |
return false; | |
} | |
document.getElementById('opener').addEventListener('click', openAnother); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment