Stop users from navigating back to index.html page on Cordova PhoneGap

I'm currently working on a web application that I access through the PhoneGap AppBrowser for mobile devices. The challenge I'm facing is with the index.html page, which I use as a splash screen but later want to prevent users from revisiting after they've been redirected to the main application page using JavaScript.

I attempted to achieve this by using "window.location.replace("www.myapp.com");", but it opened the page in the system browser instead of the desired behavior. I also tried setting up an event handler for the back button press, but it didn't work properly when returning to the index.html page.

My confusion arises from not being sure if the application opens in the Cordova webview or the app browser. I experimented with changing parameters like '_self' and '_blank' in the code, but still encountered the same issue.

How can I guarantee that clicking the back button will exit the application rather than allowing users to return to the index.html page?

Currently, I am using Cordova CLI version 6.1.1 and testing on Android 5.x.

Below is the code snippet from 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);
    }
});

Answer №1

Success! I discovered the solution here.

To exit the application when using AppBrowser, simply include this listener:

var ref = cordova.InAppBrowser.open(encodeURI(url), '_self', 'location=no');
ref.addEventListener('exit', function () {
    navigator.app.exitApp();
});

I hope this information proves helpful to others down the road.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AT&T's cutting-edge XML technology for speech recognition

I'm having trouble figuring out mutual exclusion when trying to recognize alternative rules. Let's say I want to capture commands like "play," "stop," or "set 1," "set 2." How can I achieve this? My attempt at a solution didn't work as inten ...

I'm encountering an issue in my node application where it is unable to access the push

I am a beginner in the world of node.js and express. Whenever I try to start my application using the command npm start, I encounter an error message saying Cannot Read property push of undefined from my index.js file. The problematic code snippet looks l ...

Transform date format using VueJS in JavaScript

I need to convert a date format from 19 Oct 2017 to 20171019. Is there a way to do this quickly? I am using FlatPickr in VueJs. Here is the code snippet for reference: import flatPickr from 'vue-flatpickr-component'; import 'flatpickr/dist/ ...

Efficiently organizing items within a list on Ionic

Currently, I have an ion-list structured as follows: <ion-list *ngFor = "let chat of Chats"> <ion-item (click) = "openChat(chat.id)"> <ion-label> <h2> {{chat.username}} </h2> ...

A guide to creating a dynamic ngTemplateOutlet in Angular

Below is the HTML code I attempted: HTML <ng-container [ngTemplateOutlet]="room"></ng-container> <ng-template #room1> test 1 </ng-template> <ng-template #room2> test 2 </ng-template> <ng-template # ...

I'm curious about the method used to create this body fadeIn effect

Recently, I stumbled upon the website "". I must say, it is truly remarkable. I can't help but wonder how the content on this page is cleverly faded in and slides up. This specific page caught my attention: ...

Is the browser capable of storing and managing timer IDs that are currently in use?

Is it the browser's responsibility to track active setInterval and setTimeout IDs, or is this solely left up to the developer? If the browser does keep track of them, can they be accessed through the Browser Object Model (BOM)? ...

Error with Cross-Origin Resource Sharing (CORS) upon inserting a parameter within an Express application

I'm completely stumped as to why this isn't functioning properly. My express app is deployed on Heroku and here's the code: var urlMetadata = require('url-metadata') var express = require('express') var cors = require( ...

Looking for a way to transcribe speech into text on Android in various languages? I would greatly appreciate any suggestions on how to

Is there a way to convert speech into text on an Android device using various languages? Thank you in advance. ...

Locate a specific element within a multi-dimensional array based on a partial match of one of its properties with a provided text

I am working with an array that includes three properties: ID : number Name : string Description :string ItemList :array<T>=[] and ItemListCopy :array<T>=[] Currently, this array is linked to the ng-multiselect dropdown During the onFilt ...

Is it possible to include a module-level controller within a directive?

I am currently navigating the complexities of controllers, modules, and services in Angular JS. In my attempt to integrate a controller into my directive, I faced an issue. The controller I intend to reference belongs to the same module as the directive, ...

Unable to utilize library post npm import

Recently, I attempted to integrate the flexibility library into my Laravel project. Following the installation with npm i flexibility --save, I required it in my app.js file: require('flexibility/flexibility.js'); ... flexibility(document.docume ...

Different approach to reach AngularJS isolated scope

Utilizing AngularJS isolated scope is an effective method for creating reusable components within angular directives. I find it to be quite beneficial. There are occasions when access to child scopes is necessary for data manipulation or method invocation. ...

Creating JavaScript code that writes to Response.OutputStream

I currently have a controller action that successfully generates and outputs a PDF using Response.OutputStream.Write(). Everything is functioning as expected. However, I am interested in adding another scripting section to automatically print the PDF (usi ...

How can you personalize the dropdown button in dx-toolbar using DevExtreme?

Currently, I am working with the DevExtreme(v20.1.4) toolbar component within Angular(v8.2.14). However, when implementing a dx-toolbar and specifying locateInMenu="always" for the toolbar items, a dropdown button featuring the dx-icon-overflow i ...

Can I use leaflet to customize the types of roads displayed on the map?

Is there a way to customize the types of roads displayed at different zoom levels using data from the OSM highways list? I have checked the Leaflet documentation but couldn't find the answer. If you happen to come across it, please share the link. e ...

Having difficulty accessing node_modules directory in JavaScript

I am confused about how to access node_modules for JavaScript. Can someone provide an example of calling module.exports from a node_module folder after installing a package with NPM in Node.js? File tree structure: There is a folder named 'ethereum&a ...

Is it possible to input tab characters in a TextField?

I am working with a multi-line MUI TextField and my objective is to input JSON text. However, I encountered an issue where pressing the tab key causes the component to lose focus and shift to the next element on the page. Is there a way to prevent the ta ...

Issue with RegisterClientScriptCode function following a partial postback

In a SharePoint website, the code below is contained within a user control: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Script ...

Why isn't my classList .add method working properly?

I've created a video slider background on my website, but I'm having trouble with the Javacript for video slider navigation. When I click on the button to change the video, nothing happens. The console is showing an error message that says "Canno ...