A library for easy inter-window communication encapsulated in Window:open() mehtod.
npm i open-window --save
import { openWindow, OpenerWindow } from 'open-window';
<!--publishes one browserified bundles inside the dist/ folder: -->
<script src="lib.iife.js"></script>
<!-- or -->
<script src="https://cdn.jsdelivr.net/npm/open-window/dist/lib.iife.js"></script>
In opener.html
import { openWindow } from 'open-window';
/* like as window.open*/
openWindow('http://localhost:5173/popup.html', 'popup,width=550,height=200,top=100').then(popupWindow => {
popupWindow.addCloseEventHandler(() => {
console.log('popupWindow closed');
});
popupWindow.addMessageEventHandler(msg => {
if (msg.type === 'hello') {
console.log(msg.value);
popupWindow.sendMsg({ type: 'hello', value: 'from opener.html' });
}
});
});
In popup.html
import { OpenerWindow } from 'open-window';
OpenerWindow.sendMsg({
type: 'hello',
value: 'from popup.html'
});
OpenerWindow.addMessageEventHandler(msg => {
if (msg.type === 'hello') {
console.log(msg.value);
OpenerWindow.closePopup();
}
});
function | return |
---|---|
openWindow(url[,feature]) | PopupWindow |
OpenerWindow.sendMsg | void |
OpenerWindow.closePopup | void |
OpenerWindow.addMessageEventHandler | void |
OpenerWindow.removeMessageEventHandler | void |
OpenerWindow.addCloseEventHandler | void |
OpenerWindow.removeCloseEventHandler | void |
PopupWindow | type |
---|---|
popupWindow.sendMsg | function |
popupWindow.close | function |
popupWindow.addMessageEventHandler | function |
popupWindow.removeMessageEventHandler | function |
popupWindow.addCloseEventHandler | function |
popupWindow.removeCloseEventHandler | function |
popupWindow.url | string |
popupWindow.closed | boolean |
MIT License.