65

I am new to phonegap. I have prepared one sample application. My application has 2 pages, the first page has one button, when clicked the second page will open. It is working fine using the following

         function callAnothePage()
         {
            window.location = "test.html";
         }

The second page has one button, when clicked I want to exit from the application. I used the following

       function exitFromApp()
       {
            navigator.app.exitApp();
       }

test.html code,

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">  
        function onLoad()
        {
            console.log("device reday !!!!")
            document.addEventListener("deviceready", onDeviceReady, true);
        }
        function exitFromApp()
        {
            console.log("in button");
            navigator.app.exitApp();
        }
    </script>
</head>

<body onload="onLoad();">
    <h4><center>Login Page</center></h4>

    <input type="submit" onclick="exitFromApp()" value="exit"/>

</body>
</html>

But it is not working.

user1651572
  • 753
  • 1
  • 7
  • 13

7 Answers7

70

Try this code.

<!DOCTYPE HTML>
<html>
  <head>
    <title>PhoneGap</title>

        <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>      
        <script type="text/javascript" charset="utf-8">

            function onLoad()
            {
                  document.addEventListener("deviceready", onDeviceReady, true);
            }

            function exitFromApp()
             {
                navigator.app.exitApp();
             }

        </script>

    </head>

    <body onload="onLoad();">
       <button name="buttonClick" onclick="exitFromApp()">Click Me!</button>
    </body>
</html>

Replace src="cordova-1.5.0.js" with your phonegap js .

Chirag
  • 55,270
  • 29
  • 150
  • 194
20

There are different ways to close the app, depending on:

if (navigator.app) {
    navigator.app.exitApp();
} else if (navigator.device) {
    navigator.device.exitApp();
} else {
    window.close();
}
Ujjwal Kumar Gupta
  • 1,665
  • 1
  • 14
  • 29
  • This is a nice way, thanks! Actually, I'd add a typeof check if exitApp is really a function (not sure if this would be present in all cases where navigator.app or navigator.device exist) – Peter T. Sep 21 '18 at 08:34
11
navigator.app.exitApp();

add this line where you want you exit the application.

Unihedron
  • 10,251
  • 13
  • 53
  • 66
Anil Singhania
  • 615
  • 10
  • 8
3

@Pradip Kharbuja

In Cordova-2.6.0.js (l. 4032) :

exitApp:function() {
  console.log("Device.exitApp() is deprecated. Use App.exitApp().");
  app.exitApp();
}
Shadow
  • 126
  • 1
2
if(navigator.app){
        navigator.app.exitApp();
}else if(navigator.device){
        navigator.device.exitApp();
}
Stevko
  • 3,969
  • 6
  • 36
  • 57
2

sorry i can't reply in comment. just FYI, these codes

if (navigator.app) {
navigator.app.exitApp();
}
else if (navigator.device) {
  navigator.device.exitApp();
}
else {
          window.close();
}

i confirm doesn't work. i use phonegap 6.0.5 and cordova 6.2.0

Jamz D. Mozac
  • 137
  • 2
  • 7
1

I used a combination of the above because my app works in the browser as well as on device. The problem with browser is it won't let you close the window from a script unless your app was opened by a script (like browsersync).

        if (typeof cordova !== 'undefined') {
            if (navigator.app) {
                navigator.app.exitApp();
            }
            else if (navigator.device) {
                navigator.device.exitApp();
            }
        } else {
            window.close();
            $timeout(function () {
                self.showCloseMessage = true;  //since the browser can't be closed (otherwise this line would never run), ask the user to close the window
            });
        }
Jake Lee
  • 5,837
  • 7
  • 39
  • 73
Post Impatica
  • 10,585
  • 5
  • 48
  • 61