Skip to content

Instantly share code, notes, and snippets.

@rkaw92
Created March 28, 2018 10:01
Show Gist options
  • Save rkaw92/f53d7714e11b804e2fc5b690f41bbe62 to your computer and use it in GitHub Desktop.
Save rkaw92/f53d7714e11b804e2fc5b690f41bbe62 to your computer and use it in GitHub Desktop.
Cross-window data passing in JS
<!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