Cross domain messaging made easy with window.postMessage
.
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>
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');
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');
});
This method return a postmessage type
, this can be pub
for publishing or sub
for subscribing.
var Pub = postmessage('pub');
var Sub = postmessage('sub');
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);
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();
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
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()); // -> '*'
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 specifying the origin:
pub.send({ hello: 'world' }, 'http://example.com');
// or with a target window:
pub.send({ hello: 'world' }, 'http://example.com', targetWindow);
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);
Add origin domains to white list.
sub
.origin('http://example.com')
.origin('http://simple.com')
.origin('http://hello.com');
Bind or subscribe to post messages for the specified window.
sub
.bind(function(message, event){
console.log(message);
});
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();
This unbind all listeners from subscriber and then destroy it.
sub.destroy();
First in the root directory do:
$ npm install
Then run the test like this:
$ make test
(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.