Skip to content

alanclarke/post-msg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Travis CI devDependency Status js-standard-style

post-msg

Post message made simple

  • Tiny
  • 100% test coverage
  • performant

Installation

npm install --save post-msg

Usage

  • createPostMsg(window, origin): returns a postMsg instance
  • postMsg.on(namespace, handler): listen for and handle post messages from target window
  • postMsg.emit(namespace, data): send post messages to target window
  • postMsg.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)
}

About

Post message made simple

Resources

Stars

Watchers

Forks

Packages

No packages published