Last active
February 17, 2016 20:53
-
-
Save andersevenrud/dd5b2ef49eb8c3888629 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
<!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