Post message made simple
- Tiny
- 100% test coverage
- performant
npm install --save post-msg
createPostMsg(window, origin)
: returns a postMsg instancepostMsg.on(namespace, handler)
: listen for and handle post messages from target windowpostMsg.emit(namespace, data)
: send post messages to target windowpostMsg.dispose()
: destroy postMsg instance and remove all listeners
example:
var createPostMsg = require('post-msg')
var postMsg = createPostMsg(targetWindow, '*')
postMsg.on('*', log) // listen to all post messages from the targetWindow
postMsg.on('namespace', log) // listen for namespaced post messages from the targetWindow
postMsg.emit('hello', { data: true }) // send a post message to the targetWindow
postMsg.dispose() // remove all event and post message listeners
function log (type, data, origin, source) {
console.log(type, data, origin, source)
}