I use postMessage to communicate between an host and an iframe of my domain.
Here is the code on the hostdomain.org:
function rec_tyd(json_var)
{
//if i decode and alert json_var there i get the content.
var win = document.getElementById("tyd_frame").contentWindow;
win.postMessage(json_var, "http://multipress.fr/tyd//module/pro_mod.php");
}
<iframe src="http://example.org/myiframe.php" width="500" height="200"
name="tyd_frame" id="tyd_frame" style="border:1px solid black;"</iframe>
json_var is a JSON object.
And the code on my domain, example.org/myiframe.php is:
if (!window.addEventListener) // IE
{window.addEventListener = function (type, listener, capt) {
attachEvent("on"+type, function(){listener(event)});}
}
addEventListener("message", function(e) {
var final = JSON.parse(e.data);
alert(final.id_product);
alert(final.id_company);
}, false);
Basically, everything is working except:
Same thing in Chrome and FireFox: refresh doesn't work, but if go in the address bar and press enter, it works. Except for the first time.
I checked my JSON object, and I get it before the postmessage. It's really in the iframe that I get nothing.
So my question is: how can I test that I get something? Did I made a mistake in my code?
Currently I'm actually working on the same domain but it will be on a different domain for production.
Solved in the comments :)
==> Stop launching my postMessage when document ready but put it in the onload status of the iframe and it seems that the problem is fixed ! Thanks