Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active February 17, 2016 20:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/dd5b2ef49eb8c3888629 to your computer and use it in GitHub Desktop.
Save andersevenrud/dd5b2ef49eb8c3888629 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!-- YOUR MARKUP HERE -->
<div>
<button id="Test">Test</button>
</div>
<!-- YOUR SCRIPT(S) HERE -->
<script type="text/javascript">
var osjsOrigin = {};
// When your IFRAME content has loaded
function onload(ev) {
function dotest(ev) {
if ( osjsOrigin.pid ) {
top.postMessage({
wid: osjsOrigin.wid,
pid: osjsOrigin.pid,
message: {
foo: 'bar'
}
}, '*');
}
}
// When you click the button, send message to OS.js application
document.getElementById('Test').addEventListener('click', dotest, false);
}
// When message data is recieved from OS.js
function onmessage(ev) {
function ondata(ev, data) {
console.log('Source Process ID', data.pid);
console.log('Source Window ID', data.wid);
if ( data.message === 'Window::init' ) {
osjsOrigin.wid = data.wid;
osjsOrigin.pid = data.pid;
console.log('OSjs Window inited');
} else if ( data.message === 'Window::destroy') {
console.log('OSjs Window closed');
} else {
console.log('Your custom signal here', data.message);
if ( data.message.bar ) { // Should give you "baz"
alert(data.message.bar);
}
}
}
// Validate message
if ( ev && ev.data ) {
ondata(ev, ev.data);
}
}
// Initialize
window.addEventListener('load', onload);
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