cross.js
(fungsi(global){
global.Salib = {
pengendali isyarat: {},
on: function(signal, func){
this.signalHandler[signal] = func;
},
panggilan: fungsi(menang, domain, isyarat, data, panggilan balik func){
var notice = {"signal": signal, "data": data};
if(!!callbackfunc){
notice["callback"] = "callback_" new Date().getTime();
Cross.on(notis["callback"], callbackfunc);
}
var noticeStr = JSON.stringify(notis);
win.postMessage(notisStr, domain);
}
};
$(window).on("message", function(e) {
var realEvent = e.originalEvent,
data = realEvent.data,
swin = realEvent.source,
asal = realEvent.origin,
protokol;
cuba {
protokol = JSON.parse(data);
var result = global.Cross.signalHandler[protocol.signal].call(null, protocol.data);
if(!!protokol["panggilan balik"]){
Cross.call(swin, origin, protocol["callback"], {result: result});
}
if(/^callback_/.test(protocol.signal)){
padamkan Cross.signalHandler[protocol.signal];
}
} tangkap (e) {
console.log(e);
buang Ralat baharu("ralat silang.");
}
});
})(tetingkap);
a.html