跨域信息传递postMessage

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
}
}
});
上一篇:javamail发送邮件的简单实例(转)


下一篇:记一次phpStudy apache启动后自动关闭 修改过程