HTML postmessage – cross-iframe cross-browser

23 Sep

Following my recent post on this, still lots of issues with any
solution, so instead of one of the workarounds for cross-domain
windows (
I decided to just use the official HTML5 cross-domain technique.
postMessage. Didn’t find many examples, esp an iframe calling its
parent, so here’s what worked for me in FF3:

iframe says:
top.postMessage(“this is the message i am sending”, “*”);

on page load, top window says:

if (typeof window.addEventListener != ‘undefined’) {
window.addEventListener(‘message’, onmessage, false);
} else if (typeof window.attachEvent != ‘undefined’) {
window.attachEvent(‘onmessage’, onmessage);

function onmessage(e) {

with thanks

As that blog post points out, window.onmessage() doesn’t work.

Also if using top.postMessage(), the canonical Resig example
document.addEventListener(“message”, function(e) { … } —– no go
there either. (probably because it’s a document not the window, but no
time to investigate that right now).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: