postmessage-component
v0.1.2
Published
Cross domain messaging made easy with postMessage
Downloads
16
Readme
PostMessage
Cross domain messaging made easy with window.postMessage
.
Instalation
As component:
$ component install cayasso/postmessage
Or you can just grab the postmessage.js bundle file and include it in your page:
<script src="postmessage.js"></script>
Usage
On the main window
var target = document.getElementById('myiframe').contentWindow;
var pub = postmessage('pub')();
var sub = postmessage('sub')(target);
sub.bind(function(message){
console.log(message); // -> THANK YOU
});
pub.send('HOOOOOLA');
On the iframe
var pub = postmessage('pub')();
var sub = postmessage('sub')();
sub.subscribe(function(message, event){
console.log(message); // -> HOOOOOLA
// send back to parent window
pub.send('THANK YOU');
});
API
postmessage([type])
This method return a postmessage type
, this can be pub
for publishing or sub
for subscribing.
var Pub = postmessage('pub');
var Sub = postmessage('sub');
Pub([target])
Create a publisher instance, a target window can be passed as first argument, if none the target
window will be window.parent
.
var Pub = postmessage('pub');
var pub = Pub(targetWindow);
pub.target([window]);
Target window setter and/or getter, if no argument is passed it will get the current target window, else it will set a target window.
pub.target(targetWindow);
// get the current target window
var target = pub.target();
pub.origin([domain]);
Target origin setter and/or getter, if no argument is passed it will get the current target origin, else it will set a target origin.
pub.origin('http://example.com');
// get the current target window
console.log(pub.origin()); // -> http://example.com
pub.defaults()
Reset publisher target
window and origin
to defaul values.
pub.origin('http://example.com');
// reset publisher
pub.defaults();
// get the current target window
console.log(pub.origin()); // -> '*'
pub.send(data, [target, [origin]])
Send a message to a target window. You can pass the target and origin as second and third parametters correspondingly.
// data is serialized by JSON
pub.send({ hello: 'world' });
// or with a target window:
pub.send({ hello: 'world' }, targetWindow);
// or with specifying the origin:
pub.send({ hello: 'world' }, targetWindow, 'http://example.com');
Sub([window])
Create a subscriber instance, a window can be passed as first argument, if none the default
window will be window
.
var Sub = postmessage('sub');
var sub = Sub(myWindow);
sub.origin(domain)
Add origin domains to white list.
sub
.origin('http://example.com')
.origin('http://simple.com')
.origin('http://hello.com');
sub.bind(fn)
Bind or subscribe to post messages for the specified window.
sub
.bind(function(message, event){
console.log(message);
});
sub.unbind([fn])
Unbind or unsubscribe from window post messages. If no funciton is passed it will unbind all listeners.
// unbind by passing the pointer function
sub.unbind(myFunction);
// unbind all listeners
sub.unbind();
sub.destroy()
This unbind all listeners from subscriber and then destroy it.
sub.destroy();
Run tests
First in the root directory do:
$ npm install
Then run the test like this:
$ make test
License
(The MIT License)
Copyright (c) 2013 Jonathan Brumley <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.