aralejs / messenger Goto Github PK
View Code? Open in Web Editor NEWA common iframe communication solution.
Home Page: http://aralejs.org/messenger
A common iframe communication solution.
Home Page: http://aralejs.org/messenger
https下创建iframe无src会报安全提示,建议增加一个配置用于传src。
如题,没有提供销毁的方法。可否考虑提供?
在某些需要大量创建一次性iframe通信的时候比较没有销毁处理比较头疼
如题,如果不行,如何实现: dialog(内容是iframe)中完成操作后改变父页面的状态,
比如 在dialog中登录后直接无刷新改变 '收藏'按钮的状态。
演示里怎么两个页面都是http://aralejs.org
messenger中parent与iframe的通信是独立的,而这个模块主要是将消息查模块按C/S方式进行封装,方便前台调用
seajs.use('./libs/server',function(server) {
server.init('#iframe');
});
seajs.use('./libs/client',function(client) {
client.call({'method':'get','param':'window.location.href'},function(json) {
url = json.rs || window.location.href;
alert(url);
});
});
define(function(require, exports, module) {
var Messenger = require('arale/messenger/1.0.2/messenger');
var $ = require('$');
var server = {
messenger : {},
init : function(fid) {
if(!$.isEmptyObject(parent.messenger)) {
return;
}
if(typeof(parent) == 'string') {
if($(fid).length <= 0) {
throw new Error('getElement error');
}
}
server.messenger = new Messenger({
target: fid,
onmessage: function(json) {
if(typeof(json) != 'object' || !json.orderno) { //消息格式
return;
}
orderno = json.orderno;
result = {};
result.orderno = orderno;
result.param = json.param;
//處理消息
switch(json.method) {
case 'get':
eval('result.rs=' + json.param);
this.send(result);
break;
case 'call':
eval('result.rs=' + json.param + '(' + json.opts + ')');
this.send(result);
break;
}
}
});
}
};
module.exports = server;
});
define(function(require, exports, module) {
var Messenger = require('arale/messenger/1.0.2/messenger');
var $ = require('$');
var client = {
//client 2 server
call : function(opts,callback) {
if(!opts.method) {
throw new Error('opts method is not exists');
}
var i = 0;
do
{
date = new Date();
orderno = date.getTime();
}
while (!client.dict.hasOwnProperty(orderno) && i++<=5);
if(client.dict.hasOwnProperty(orderno)) {
throw new Error('iframe dict create fail');
}
client.dict[orderno] = callback;
//發送消息
if(!$.isEmptyObject(client.messenger)) {
client.init();
}
opts.orderno = orderno;
client.messenger.send(opts);
},
//消息初始化,在base加載
init : function() {
if(!$.isEmptyObject(client.messenger)) {
return;
}
fid = arguments[1] || parent;
client.messenger = new Messenger({
target: parent,
onmessage: function(json) {
if(typeof(json) != 'object' || !json.orderno) { //消息格式
return;
}
if(typeof(parent) == 'string') {
if($(fid).length <= 0) {
throw new Error('getElement error');
}
}
orderno = json.orderno;
callback = client.dict[orderno];
if(!callback) {
return;
}
delete client.dict[orderno];
callback(json);
}
});
}
};
client.dict = new Array(); //消息字典:orderno、callback
client.messenger = {};
module.exports = client;
});
var targetFunc = window.navigator[prefix + this.name];
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.