I am a new Cordova web Developer , I 'm creating an application that load automatically a web site using inapbrowser ,my probleme is : after loading the site when i click on the backbutton , instead of closing the application , it goes back to the blank page that contains the inapp browser , how to do to quit the application when i press the back button ??
Asked
Active
Viewed 2,212 times
0
-
Hi Imen, suggest you to have a look at this link - http://stackoverflow.com/help/how-to-ask If you dont follow this, you may eventually end up getting loads of down votes for your question and end up losing your reputation and the question will be closed. I know you are new to this but i m telling this as i too faced the same issue earlier – Gandhi Jul 27 '16 at 04:53
1 Answers
0
You can probably register a backbutton event on InAppBroswer page load and exit the app on click of back button. The sample code is as follows:
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
//inapp is a sample button's id available in HTML
$('#inapp').click( function()
{
try {
ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener("exit", onBackButton, false);
}
catch(err) {
alert("Plugin Error - " + err.message);
}
});
function onBackButton(e) {
alert("back button pressed");
navigator.app.exitApp();
}
}
The key to your question lies in the addEventlistener section of the Official InAppBrowser Plugin Page.

Gandhi
- 11,875
- 4
- 39
- 63
-
I tried to use this sample but it's not working , i have this error **"Uncaught TypeError: Cannot call method 'confirm' of undefined"** – Imen.Ab Jul 27 '16 at 08:15
-
@Imen.A You gotta install cordova-plugin-dialogs plugin to use navigator notification. Else you can use simple javascript alertbox too – Gandhi Jul 27 '16 at 10:00
-
here is my code without navigator notification , what's the problem `document.addEventListener('deviceready', function(){ cordova.InAppBrowser.open('http://example.com', '_blank', 'location=no'); document.addEventListener("backbutton", onBackKeyDown, false); }); function onBackKeyDown() { navigator.app.exitApp(); }` – Imen.Ab Jul 27 '16 at 10:03
-
@Imen.A Please find the updated answer that was tested in android device – Gandhi Jul 27 '16 at 10:31
-
@Imen.A Have updated the code in github project too which i built for you earlier - https://github.com/gandhirajan/Cordova_InAppBrowser – Gandhi Jul 27 '16 at 10:48