How to HTML/Javascript postMessage call function

Main page

var iframe = document.getElementById("iframeId");
iframe.contentWindow.postMessage(JSON.stringify
			({"namespace":"YourNamespace",
			 "function": "YourFunction",
			 "args": [arg1, arg2, arg3]
			}), '*');

Child Page

function receiveMessage(evt)
{
        var data = JSON.parse(evt.data);
    	if(data.namespace == "YourNamespace" && data.function == "YourFunction")
	{
        	YourNamespace.YourFunction( data.args[0], data.args[1], data.args[2] );
    	}
}
window.addEventListener("message", receiveMessage, false);
Last modified: le 2018/11/16 12:39