PostComm.js @ Github

Both for my work with advanced forms at Ciere Consulting, and for my own projects, I needed the ability to securely and easily operate with many iframes simultaenously. I built a library that generates objects that abstract away the connection with an individual iframe or child window and provides routing of messages to the appropriate handler.

The library provides a very simple way to set up an iframe or child window connection. The differences between connections with iframes, child windows, or a parent window are abstracted away, as are the differences between cross-domain and same-origin connections. Once the connection is created, it can be tested to ensure the connection is set up on both sides. PostComm ensures security by always checking the origin and contentWindow properties before calling any handler functions.

By wrapping the connection's functionality and interaction into a single object for each connection, I can communicate through the connection, give the connection to another part of the program, and delete the connection at any time without needing a reference to the library that created it.

This "microlibrary" is my first open source Javascript project. It is fully unit tested and documented. Development continues as I learn better ways of improving security with parent windows and testing connections. Internet Explorer support is currently very spotty and should be improved. Furthermore, I will explore the new MessageChannel standard as it becomes implemented.

Below is the all that is required to create, test, and send a message on a secure, routed connection at its most complicated. There are more convenient shortcuts in normal usage.

// Create message connection with iframe or window
var myComm = postComm.createComm(aUrl, aContentWindow, onMessage);

function onMessage(message, comm) {
    // Handle messages received from the iframe or window

function onSuccess(comm) {
    // The connection is confirmed
function onFailure(comm) {
    // The connection test failed

// Test the connection, onFailure);

// Send a message to the iframe or window