framehost - A lightweight cross domain communication library
Support data communication between iframe, browser new tab windows (Note: IE10+)
Install
npm install framehost --save
How to use?
;
1.iframe code example
// parent.js const iframe = document; // Define actions// When the message is received, the matching method is triggered. The parameter is the received data. Multiple methods can be defined in action objects.const actions = { console; } // You can set up a secure domain name source, default to '*'.const origin = 'http://www.children.com'; // Init framehostconst framehost = actions origin iframecontentWindow; // Send a message to the iframe window, execute the 'doChildrenAction' method defined in iframe, and pass the data to it. The data can be object, number, string, boolean, array.framehost;
// children.js const actions = { console; }const origin = 'http://www.parent.com';const framehost = actions origin; framehost
2.browser tab windows code example
This is basically the same as the iframe sample code above, except that the third window parameter passed when the FrameHost is initialized is different.
// pageA const actions = { console; }const origin = 'http://www.pageB.com'; const targetWin = window;const framehost = actions origin targetWin; framehost
// pageB const actions = { console; }const origin = 'http://www.pageA.com'const framehost = actions origin; framehost
API
new FrameHost(actions: Object, [origin: String], [targetWin: Window])
Initializing FramHost can receive three parameters:
actions
Type is an object and keys are functions. When a message is received, the key in the object is matched and the corresponding method of key is executed, such as:
const actions = { console; } { console; } { console; };
origin
Message source, when the origin
parameter is provided, only messages from the domain name set by origin are processed. default to '*'.
targetWin
The target window for message sending, if the message is sent to the iframe window, the parameter value is iframe.contentWindow
. If the message is sent to the browser tab window, the parameter value is the window reference returned after the window.open()
method is executed.
postMessage({action: String, data: Any}, callback: Function)
postMessage([{action: String, data: Any}, ...], callback: Function)
The method of sending messages is an instance method of FrameHost
. This method receives an object or an array of objects and a callback function as parameters. The parameter object must contain action
and data
fields, action
represents the method name that the target window needs to execute, and data
represents the data transferred. The callback
function will be called immediately after all messages are sent out, and callback
is optional. Such as:
const framehost = actions origin targetWin;framehost; // or/*framehost.postMessage([ { action: 'doSomething1', data: 'I am a message data', }, { action: 'doSomething2', data: 'I am a another message data', },], () => { console.log('all message sent');});*/
destroy()
If it is determined that there is no need to continue sending messages, the destroy
function can be released. Note: after calling the function, you cannot continue sending messages. Such as:
framehost; // The following code will not continue to work because the destroy method has been invoked before.framehost;
Example
A working example can be found in the example
directory,Please ensure that domain name mapping is set up locally.
127.0.0.1 example.a.com
127.0.0.1 example.b.com
run npm run example
,And open http://example.a.com:9000/iframe or http://example.a.com:9000/window in browser.