postmate
postmate 是一款基于postMessage来处理父子页面通信的库,轻量且好用。
详细的使用见 示例
postmate的不足
postmates-js的改进
// Kick off the handshake with the iFrame
PostmatesJS.debug = true;
const open = window.open('http://localhost:8083/c3.html', '_blank')
const handshake = new PostmatesJS([
container: document.getElementById("cid1"), // first way
url: "",
name: "name1"
container: document.getElementById("cid2"), // second way, similar to `postmate`
url: "http://localhost:8082/c2.html",
name: "name2"
container: open, // document.getElementById("cid2"), // third way, open a new page with `window.open`
url: "http://localhost:8083/c3.html",
name: "name2"
// When parent <-> child handshake is complete, data may be requested from the child
handshake.then(parentAPIs => {
parentAPIs.forEach(parentAPI => {
parentAPI.on('some-event', data => {
console.log(data)
}); // Logs "Hello, World!"
parentAPI.call("demoFunction", {options:"Hello, PostmatesJS!"})
localhost:8081/c1.html
PostmatesJS.debug = true
const model = new PostmatesJS.Model({
demoFunction:(options) =>{
console.log('child1', options)
model.then(childAPI => {
childAPI.emit('some-event', 'Hello, World! Child1');
localhost:8082/c2.html
PostmatesJS.debug = true
const model = new PostmatesJS.Model({
//demoFunction:提供给父页面的方法
//options: 从父页面传入的参数信息
demoFunction:(options) =>{
console.log('child2', options)
model.then(childAPI => {
childAPI.emit('some-event', 'Hello, World! Child2');
localhost:8083/c3.html
PostmatesJS.debug = true
const model = new PostmatesJS.Model({
//demoFunction:提供给父页面的方法
//options: 从父页面传入的参数信息
demoFunction:(options) =>{
console.log('child3', options)