仅当 iFrame 中的 html 页面来自与父页面相同的应用程序时,这才有效。或者至少协议、子域、域和端口都是相同的。
您需要在父页面中公开一个全局函数,该函数将由在子页面 (iframe) 中运行的 JavaScript 调用。
在您的子页面中,您调用:
if (window.parent && window.parent.myGlobalFunction) {
window.parent.myGlobalFunction('hello!');
}
在您的父页面中,您包含以下全局函数(当然可以根据需要命名):
function myGlobalFunction(input){
console.log('message received:'+input);
MyApp.app.fireEvent('closeMyWindow',input);
}
这假设您正在使用 MVC,并且您创建了属性“app”并在应用程序启动功能中将其设置为“this”。并且您有一个控制器正在侦听这样的应用程序范围的事件:
Ext.define('MyApp.controller.Qnum', {
extend:'Ext.app.Controller',
init:function(){
this.control({
...
});
//listen for app wide event fired by : MyApp.app.fireEvent('closeMyWindow',input);
this.application.on({
closeMyWindow: this.closeWindowItsDrafty,
scope: this
});
},
closeWindowItsDrafty:function(){
//get reference to my window
//call myWindow.close();
}