I need to test out a situation where I have a parent window hosting an iframe which will be empty. I need to use the iframe as a proxy between the parent and a cross-domain resource. I've created a simple jsfiddle that should attempt to post a message from the parent to the child iframe.
What I try to do in the parent's javascript is have a handler for the child iframe, but for the life of me I cannot figure out the syntax to get it working correctly. I have tried using an addEvent helper to get the event properly added (for IE and everybody else) from examples I've seen in Modern Javascript Develop and Design:
addEvent: function(obj, type, fn) {
if (obj && obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj && obj.attachEvent) {
obj.attachEvent('on' + type, fn);
}
}
The way I'm currently trying to setup the handler for onmessage is as follows:
document.getElementById("frame1").onmessage = function(e) {
alert(e.data);
};
I have also thought that maybe I need to be accessing the element's contentWindow, but onmessage does not seem to exist.
Is there a property or way to access an iframe such that it appears like a window and would have an onmessage that could be handled?
Update
I can see the value of onmessage set to a function and even alert(...) out the value to see that it is indeed my intended function, but I do not see it called when postMessage is called on the child iframe.