Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 084d9491c3
Fetching contributors…

Cannot retrieve contributors at this time

39 lines (28 sloc) 1.41 kb

PostMessager

PostMessager is a MooTools plugin that acts as a wrapper for the window.postMessage API which is available in IE8+, Firefox 3.1+, Opera 9+, Safari, and Chrome. PostMessager also normalizes the onMessage event for use within MooTools.

Screenshot

How to Use

PostMessager instances can be created at any time. Two arguments are accepted: the destination window and the instance options.

#JS

/* Get hold of an iFrame */
var domain = 'http://domain2.com';
var iframe = document.id('listenerFrame').contentWindow;

/* Create a PostMessager instance */
var messager = new PostMessager(iframe,{
    allowReceive: true,
    allowSend: true,
    validReceiveURIs: ['http://domain2.com'],
    onSend: function(message,dest) {
        console.log('sending "',message,'" to ',dest);
    },
    onReceive: function(message,source,origin) {
        console.log('received message "',message,'" from ',origin,' at ',source);
        this.reply('Got it!',source,origin);
    }
});

/* Send a message to the iFrame! */
messager.send('Hello from the parent window!',domain);

PostMessager handles sending, receiving, and replying to messages. Optimally you would add an instance to each frame.

For specific usage and options, please read the documentation or visit http://davidwalsh.name/js/postmessager

Jump to Line
Something went wrong with that request. Please try again.