插件用於這麼幾種狀況javascript
1.經過window.opener (子頁面向父頁面發送數據)html
2.經過window.open (子頁面向父頁面發送數據)java
3.嵌入iframe (父頁面向iframe中頁面發送數據)jquery
/** * 客戶端通用消息門戶框架 * @returns {undefined} */ (function($) { "use strict"; var MPBUS = { /** * 路由總線 */ routerBus: {}, /** * 消息管理的窗口列表 */ windows: [], /** * 消息總線的觸發器 * @param {type} event 事件類型 * @param {type} data 事件數據 * @param {type} type 數據類型 * @returns {unresolved} */ _fire: function(event, data, type) { var me = this; jQuery.each(this.windows, function(i, v) { if (Object.prototype.hasOwnProperty.call(v, 'myMP') /*v.hasOwnProperty('myMP')*/ && v.myMP !== null && v.myMP !== undefined) { v.myMP._fire(event, data, type); } }); //處理路由 jQuery.each(this.routerBus, function(k, v) { if (k === event) { var target = v.target; var func = v.func; var newData = func.call(this, data, type); if (newData != undefined && newData != null) { var targetType; var targetData; if (Object.prototype.hasOwnProperty.call(newData, 'type') /*newData.hasOwnProperty('type')*/ && newData.type != undefined) { targetType = newData.type; } else { targetType = type; } if (Object.prototype.hasOwnProperty.call(newData, 'data')/*newData.hasOwnProperty('data')*/ && newData.data != undefined) { targetData = newData.data; } else { targetData = newData; } me._fire(target, targetData, targetType); } } }); } }; var myMP = { /** * 消息總線所在窗口 */ BUSWINDOW: undefined, /** * 本窗口消息總線 */ eventBus: {}, /** * 得到窗口的父窗口 * @param {type} win * @returns {unresolved} */ parentWindow: function(win) { if (win === undefined || win === null) return null; var parent = win.opener; if (parent && parent !== win) return parent; parent = win.parent; return parent !== win ? parent : null; }, /** * 得到消息總線窗口 * @param {type} win * @returns {@exp;win@pro;myMP_Windows} */ _getBusWindow: function(win) { if (win === undefined || win === null) return null; if (jQuery.isPlainObject(win.MPBUS)) { return win; } else { var pwin = this.parentWindow(win); return this._getBusWindow(pwin); } }, /** * 將自身窗口註冊到消息總線中 * @param {type} win * @returns {undefined} */ register: function(win) { var busWindow = this._getBusWindow(win); if (busWindow === undefined || busWindow === null) { window.MPBUS = MPBUS; busWindow = window; } var haswindow = false; jQuery.each(busWindow.MPBUS.windows, function(i, v) { if (v === win) haswindow = true; }); if (!haswindow) busWindow.MPBUS.windows.push(window); this.BUSWINDOW = busWindow; }, /** * 向消息門戶發送事件 * @param {type} event 事件類型 * @param {type} data 事件數據 * @param {type} type 數據類型 * @returns {unresolved} */ send: function(event, data, type) { if (jQuery.isWindow(this.BUSWINDOW)) { this.BUSWINDOW.MPBUS._fire(event, data, type); } }, /** * 消息註冊方法 * @param {type} event * @param {type} func * @returns {undefined} */ on: function(event, func) { this.eventBus[event] = func; }, /** * 消息路由方法 * * @param {type} event 源消息名稱 * @param {type} target 目標消息名稱 * @param {type} func 數據轉換方法,返回的數據使用 {data:newData,type:newType }形封裝 * @returns {undefined} */ route: function(event, target, func) { if (jQuery.isWindow(this.BUSWINDOW)) { this.BUSWINDOW.MPBUS.routerBus[event] = {target: target, func: func}; } }, /** * 本窗口事件觸發器 * @param {type} event * @param {type} data * @param {type} type */ _fire: function(event, data, type) { var me = this; jQuery.each(this.eventBus, function(k, v) { if (k === event) { v.call(me, data, type); } }); }, /** * 反註冊窗口 * @param {type} win */ unregister: function(win) { var busWindow = this.BUSWINDOW; if (jQuery.isWindow(busWindow)) { var deleteIndex = -1; for (var i = 0; i < busWindow.MPBUS.windows.length; i++) { if (busWindow.MPBUS.windows[i] === win) { deleteIndex = i; break; } } } if (deleteIndex >= 0) { busWindow.MPBUS.windows.splice(deleteIndex, 1); } } }; myMP.register(window); window.myMP = myMP; })(jQuery);
例子 :windows
父頁面:框架
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="jquery-msg.js"></script> <script type="text/javascript"> (function(){ myMP.on("move", function(data,type) { console.log("from opener: x座標:"+data.x+",y座標:"+data.y); }); })(); </script> </head> <a href="on.html" target="_blank" >類型1: window.opener</a> <body> </html>
子頁面:ui
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="jquery-msg.js"></script> <script type="text/javascript"> (function(){ document.onmousemove = function(ev) { myMP.send('move', {x: ev.pageX, y: ev.pageY},'test'); }; })(); </script> </head> <h1>經過超連接打開</h1> <body> </html>
結果:this