首先简要概述下这个API的功能:在指定window或者frame中执行脚本,对于frameGroup里面的frame也有效,若name和frameName都未指定,则在当前window中执行脚本
官方给出的示例代码:
//在名为winName的window中执行jsfun脚本
var jsfun = 'funcGoto();';
api.execScript({
name: 'winName',
script: jsfun
});
//在名为winName的window中找到
//名为frmName的frame,并在该frame中执行jsfun脚本
var jsfun = 'funcGoto();';
api.execScript({
name: 'winName',
frameName: 'frmName',
script: jsfun
});
//在当前window中找到
//名为frmName的frame,并在该frame中执行jsfun脚本
var jsfun = 'funcGoto();';
api.execScript({
frameName: 'frmName',
script: jsfun
});
接下来说一说这个方法在项目中的实际应用.
应用场景:
如上图所示,当选择入学时间,毕业时间的时候,需要从下方空白处弹出一个新的frame,在新的frame中添加模块控件
UICustomPicker,代码如下:
apiready = function(){
var text = api.pageParam.text4;
varUICustomPicker = api.require('UICustomPicker');
// if(UICustomPicker == nil){
UICustomPicker.open({
rect: {
x: 10,
y: 0,
w: 320,
h: 100
},
styles: {
bg: 'rgba(0,0,0,0)',
normalColor: '#959595',
selectedColor: '#3685dd',
selectedSize: 36,
tagColor: '#3685dd',
tagSize: 10
},
data: [{
tag: '年',
scope: '1980-2020'
}, {
tag: '月',
scope: ["01","02","03","04","05","06","07","08","09","10","11","12"]
}, {
tag: '日',
scope: ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]
}],
rows: 3,
id:1,
fixedOn: api.frameName,
fixed: true
}, function( ret, err ){
if( ret ){
// alert( JSON.stringify( ret ) );
time = ret.data[0]+"-"+ret.data[1] + "-" + ret.data[2];
//使用execScriptAPI,将时间作为参数进行传递.
var param = { date: time};
api.execScript({
name: 'win_addExperience.html',
script: 'funcGoto('+JSON.stringify( param )+')'
});
}else{
// alert( JSON.stringify( err ) );
}
});
};
此时点击时间输入框,就有上图所示的效果了.最终想要的是想将模块控件的数据 填写到window的输入框,这里就要用到主人公execScript这个API接口了.
整个过程大致是这样的:在时间Picker中选好时间,直接将时间作为json格式的参数,通过execScript在父window执行script方法,从而实现功能.
//frame 中execScript方法
//使用execScriptAPI,将时间作为参数进行传递.
var param = { date: time};
api.execScript({
name: 'win_addExperience.html',
script: 'funcGoto('+JSON.stringify( param )+')'
});
//win_addExperience.html中的script方法
function funcGoto(time){
text4.value = time.date;
}
这样,就可以实现所要的功能了,还是非常方便的!