Question

Cordova 2.0.0

The following API call doesn't work on Chrome browser, as expected;

navigator.app.exitApp();

I'm getting

TypeError: Cannot call method 'exitApp' of undefined

My code is simply including the cordova-2.2.0.js file. No further actions taken regarding PhoneGap.

On the mobile device this API call does work.

So I'm guessing there's an additional step I should take in order to abstract this sort of API functions??? Maybe some sort of "require" call?

Edit #1:
I think it wasn't clear enough: I was hoping that Cordova would abstract this sort of API so that even if not really available (i.e not under a real device but under Chrome instead), these calls would do nothing (especially exitApp).

In addition - I can see navigator object in Chrome inspection, which is of type CordovaNavigator.

Was it helpful?

Solution

Well, you're trying to use cordova native calls on chrome, wich of course won't run, cordova native calls are made to access native mobile features, wich Chrome doesnt emulate.

Also i recommend you to use always the lastest stable version, cordova is already on version 2.8

You can get the chrome addon ripple emulator to test cordova apps on it, still it doenst support some cordova features : http://emulate.phonegap.com

OTHER TIPS

This answer relies on the ripple emulator though you probably wanted a more general solution.

Two things. First, ripple is new and improved but it still has this error. You can see this article: http://www.raymondcamden.com/index.cfm/2013/11/5/Ripple-is-Reborn and follow links to get the new ripple from github: https://git-wip-us.apache.org/repos/asf?p=incubator-ripple.git;a=blob_plain;f=README.md;hb=HEAD

Second, you can fix ripple to prevent that particular error, which still exists even though ripple is much improved for working with cordova 3.0. Here's what I did after all the install process: 1) find the ripple.js file; for me it was at C:\Documents and Settings\myusername\Application Data\npm\node_modules\ripple-emulator\pkg\hosted\ripple.js

2) Find this line "ripple.define('platform/cordova/2.0.0/bridge/app', function (ripple, exports, module) {" which was at line #32611 in my download.

3) After the comments following that line, add the exitApp() function with the existing show() function. I used this code:

module.exports = {
    show: function (success) {
        return success && success();
    },
    exitApp: function(){
        if(console && console.log) {
            console.log("Tried to exit app from within ripple.");
        }
    }
};

I get the feed back in the console that the app tried to exit. You could do something else that is useful to you. I think the new ripple is worth the work of installing it. From there, you can fix it yourself or even contribute a useful solution.

UPDATE: exitApp and overrideBackbutton have both been added to ripple (in same place in the code as mentioned above). See this link: Add App.exitApp and App.overrideBackbutton methods support

  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
            <script src="js/jquery.mobile-1.4.2.js"></script>
            <script src="js/cordova.js"></script>
            <script>
                document.addEventListener("deviceready", onDeviceReady, false);

                function onDeviceReady() {
                    document.addEventListener("backbutton", onBackClickEvent, false);
                }

                function onBackClickEvent() {
                    if (navigator.app) {
                        navigator.app.exitApp();
                    } else if (navigator.device) {
                        navigator.device.exitApp();
                    }
                }

            </script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top