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 (http://softwareas.com/cross-domain-communication-with-iframes)
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 http://austinchau.blogspot.com/2008/11/html5-cross-document-messaging.html

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

Also if using top.postMessage(), the canonical Resig example
http://ajaxian.com/archives/cross-window-messaging-with-html-5-postmessage
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).

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: