my-messenger
is an enhanced tool for iframe communication, which is light, simple and reliable to use
npm i my-messenger -S
parent.html
import Messenger from 'my-messenger';
// Messenger.Parent.debug = true; // open if you want debug info
const iframe = document.getElementById('iframe');
const parent = new Messenger.Parent(iframe);
parent.connect()
.then(() => {
console.log('>>> connected');
parent.send('greet', 'hi');
})
.catch(e => {
console.log('>>> connect failed', e);
});
child.html
import Messenger from 'my-messenger';
// Messenger.Child.debug = true; // open if you want debug info
const child = new Messenger.Child();
child.connect()
.then(() => {
console.log('>>> child.connected');
child.on('greet', (data) => {
console.log('>>> from parent', data);
});
})
.catch(e => {
console.log('>>> child.connect failed', e);
});
el: Element of Child iframe or a iframe Selector
connect to Child
send msg to Child
listen to event from Child
clean and close
connect to Parent
send msg to Parent
listen to event from Parent
clean and close