Cordova PhoneGap-阻止用户返回index.html页面
Cordova PhoneGap - Prevent user from returning to index.html page.
我有一个web应用程序,我正在使用PhoneGap AppBrowser访问它。
我使用index.html页面作为启动屏幕页面,并希望防止用户在通过该页面上运行的JS重定向到应用程序页面后再返回该页面。我曾尝试使用window.location.replace("www.myapp.com");
进行此操作,但这会在系统浏览器中打开页面。
我尝试设置一个事件处理程序,当按下后退按钮时会调用它,但当我返回到此页面时,它不起作用(我必须在到达此页面时按下后退按钮才能调用它(。老实说,我对应用程序是在Cordova网络视图中打开还是在应用程序浏览器中打开有点困惑。我尝试将cordova.InAppBrowser.open(encodeURI(url), '_self', 'location=no');
更改为cordova.InAppBrowser.open(encodeURI(url), '_blank', 'location=no');
,但最终还是得到了相同的结果。
我如何确保用户在单击后退按钮时不能返回到index.html页面,而是退出应用程序?
我正在使用Cordova CLI 6.1.1并在Android 5.x上进行测试。
index.js如下:
$(function () {
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);
var url = 'http://www.myapp.com';
window.open = cordova.InAppBrowser.open;
setTimeout(function () {
var ref = cordova.InAppBrowser.open(encodeURI(url), '_self', 'location=no');
}, 2500);
};
function onBackKeyDown(e) {
e.preventDefault();
console.log(document.location.href.indexOf('asset/www/index.html') > 1);
}
});
明白了,在这里找到了答案。
只需添加以下侦听器即可在AppBrowser退出时退出应用程序。
var ref = cordova.InAppBrowser.open(encodeURI(url), '_self', 'location=no');
ref.addEventListener('exit', function () {
navigator.app.exitApp();
});
希望这对将来的其他人有所帮助。
相关文章:
- 分析高度属性时出现意外值{{specs.height}}.index.html
- AngularJS-需要在index.html页面中访问来自服务的数据
- index.html使用webpack开发服务器未重新加载
- 从index.html调用函数,该函数无限循环
- RouteChangeStart事件在初始index.html页面加载时触发
- Angular2:Index.html没有't显示该文件;正在加载&”;
- 如何使用gulp-inject将文件的内容插入到我的index.html中
- 使url匹配'/''/索引'和'/index.html'到单一状态
- 将所有angular2捆绑包包含在index.html中会使导入从内存获取数据,而不是再次从服务器请求数据
- 咕哝式发球;t将index.html与'/'要求
- AngularJS应用程序:如何将.js文件包含到index.html中
- 无法从index.html调用javascript函数
- 捕获刷新并将页面更改为index.html
- 添加java脚本后,Dropdown Menu无法在Index.html上工作
- jquery手机应用程序的第一页必须是index.html
- Angular 2-我如何使我的index.html文件的标题和关键字和描述的元标签是动态的
- 使index.html的默认状态不带#something
- 从'更改不同语言的图像;index.html'到'关于.html'
- ng递归地包括整个index.html;达到“10$digest()迭代”;
- angular-index.html只识别默认路由中定义的函数