Skip to content

Instantly share code, notes, and snippets.

@akash1810
Last active January 27, 2017 09:48
Show Gist options
  • Save akash1810/dda67e9c7b66c326fe2b9d41bc9fe7c0 to your computer and use it in GitHub Desktop.
Save akash1810/dda67e9c7b66c326fe2b9d41bc9fe7c0 to your computer and use it in GitHub Desktop.
Chrome wat?!
<!DOCTYPE html>
<html>
<head></head>
<body>
<button type="button" onclick="window.parent.postMessage({name: 'foo'}, '*');">
Press Me
</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
Name = <span id="val"></span>
</div>
<iframe src="iframe.html" style="border: 0;"></iframe>
<script>
function setVal() {
// Firefox (correctly) thinks `event` is `undefined`
// Chrome, Safari, Opera think `event` is an `Object`... WAT?!
document.querySelector('#val').textContent = event.data.name;
}
function onMessage (event) {
if (event.origin !== window.location.origin) {
return;
}
setVal();
}
window.addEventListener('message', onMessage, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment