Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

PostMessager is a MooTools wrapper for the window.postMessage API.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Docs
Octocat-spinner-32 Source
Octocat-spinner-32 tests
Octocat-spinner-32 README.md
Octocat-spinner-32 package.yml
README.md

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

Something went wrong with that request. Please try again.