var sendToParent = function(event, data, listener) { var message = { event: event, data: data, listener: listener }; window.parent.postMessage(JSON.stringify(message), "*"); };
var iframe = document.getElementById('iframe'); var sendToIframe = function(event, data, listener) { if (iframe && iframe.contentWindow) { var message = { event: event, data: data, listener: listener }; iframe.contentWindow.postMessage(JSON.stringify(message), '*') } };
var bindEvent =function (el, event, listener, useCapture) { 'use strict'; if (typeof useCapture === 'undefined') { useCapture = false; } if (el.addEventListener) { el.addEventListener(event, listener, useCapture); } else if (el.attachEvent) { el.attachEvent('on' + event, listener); } };
bindEvent(window, "message", function(e){ var data = JSON.parse(e.data)||{}; if(data.event && (typeof events[data.event] == "function") ){ events[data.event](data.data||{}); if(data.listener){ //callback } } });