Messager is a lightweight (1kb Gzipped) dependancy free wrapper for postMessage API. Messager allow you to send event to and from an iframe, cross domain or not.
Just include the minified version of MiniMasonry.js on your website :
<script src="messager/build/messager.min.js"></script>
Messager should be instanciated on both iframe and parent page.
On the host website :
var iframe = document.getElementById('myFrame');
var messager = new Messager({
remoteWindow: iframe.contentWindow,
local: "http://hostaddress.com/",
remote: "http://iframeaddress.com/"
});
On the iframe's website :
var messager = new Messager({
remoteWindow: window.parent,
local: "http://iframeaddress.com/",
remote: "http://hostaddress.com/"
});
Then, from the iframe or the host website, you can send event like this :
messager.send('eventName', {param: 'value'});
And listening to this event like that :
messager.on('eventName', callback);
The callback function will receveive the given parameters as argument.
Here is the list of available parameters :
Name | Default value | Description |
---|---|---|
local | null | Hostname of the current website (iframe hostname or parent hostname). Required |
remote | null | Hostname of the remote website (iframe hostname or parent hostname). Required |
remoteWindow | null | Window object of the remote website. Required |
window | window | Window object of the local website. |
Here is the list of available APIs :
Name | Description |
---|---|
send('eventName', parameters) | Function to send event and parameters to the iframe / host. |
on('eventName', callback) | Callback to execute on the iframe or host to execute when receiving an event. |
MIT
Made by Spope