mui项目总结第1篇---窗口的获取以及关闭

345 阅读1分钟

关于获取webview(窗口间关系)的方法

1.获取指定页面ID的webview

plus.webview.getWebviewById('为页面设置的id值');

2.获取当前页面的webview

plus.webview.currentWebview();
var ws=plus.webview.currentWebview();//获取当前页面所属的Webview窗口对象

console.log( "窗口标识: "+ws.id );

console.log( "当前Webview窗口:"+ws.getURL() );

3.获取当前页面的父级

plus.webview.currentWebview().opener();

4.获取所有的可视的webview窗口

plus.webview.all();

5.获取应用显示栈顶的WebviewObject窗口对象

plus.webview.getTopWebview();

关闭窗口

注意:方法要写在plusready方法内部

document.addEventListener("plusready", onPlusReady, false);
			
function onPlusReady(){       
	var curr = plus.webview.currentWebview();   //获取当前页面
	var wvs = [];       //空数组用来储存已经获取的窗口
	if(plus.webview.getWebviewById('my_vip0.html')){
	    //首先判断该页面否已经打开否则不添加
		wvs.push(plus.webview.getWebviewById('my_vip0.html'));
	}
	if(plus.webview.getWebviewById('my_vip1.html')){
	    //首先判断该页面否已经打开否则不添加
		wvs.push(plus.webview.getWebviewById('my_vip1.html'));
	}
	if(plus.webview.getWebviewById('my_vip2.html')){
	    //首先判断该页面否已经打开否则不添加
		wvs.push(plus.webview.getWebviewById('my_vip2.html'));
	}
	console.log(wvs);
	for (var i = 0,len = wvs.length; i < len; i++) {
  		//关闭除当前页面外的其他页面
  		//关闭方法一
  		if (wvs[i].getURL() != curr.getURL()){
			plus.webview.close(wvs[i],"none");
  		}
  		//关闭除指定页面外的其他页面
  		//key 可以是指定页面名称或者其他关键字
  		//关闭方法二
		if (wvs[i].getURL().indexOf("Key") == -1){
			plus.webview.close(wvs[i]);
		}
	}
	
}