Arun
Arun

Reputation: 685

How can i call phonegap functions in html

I'm building a basic html application for mobile using jquery mobile and phonegap build. i have a button for closing the application in mobile. i got the code for closing the application.

my code is :

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

I build my application with phonegap build . and tested it in my android phone but the code is not working for me.

Can you tell me is it possible to use phonegap functions in build phonegap.

if possible how can i do that?

Upvotes: 0

Views: 983

Answers (3)

taruna-systematix
taruna-systematix

Reputation: 111

I have used the following code and its working fine for me ;-

document.addEventListener("deviceready", deviceready, false);
function deviceready() {
    document.addEventListener("backbutton", onBackKeyDown, false);
    function onBackKeyDown() {
        if (confirm("Do you want to Exit?")) {
            navigator.app.exitApp();
        }
    }
}

Upvotes: 0

Ataru
Ataru

Reputation: 544

Please run the code below on your PhoneGap App. You need to wait 'deviceready' event to use PhoneGap ApI. Then, you should add event listener to any elements. When 'deviceready' event does not come, please make sure whether your cordova.js is loaded.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">

<!--This line should be updated according to your cordova.js version-->
<script src="cordova.js"></script>

<script>

    document.addEventListener('deviceready', onDeviceready, false);

    function onDeviceready(){

        console.log('Now you can use PhoneGap APIs');

        document.getElementById('exit_button').addEventListener('click', closeThisApp, false);

            console.log('Add click event to the button and call app.exitApp');
            function closeThisApp(){
                navigator.app.exitApp();
            }

    }

</script>
</head>
<body>

<button id="exit_button">Exit</button>

</body>
</html>

Upvotes: 0

Shubham
Shubham

Reputation: 187

You need to add a listener event for the closebutton and associate a function.

Create a listener like this

document.addEventListener("closebutton", close_app, true);

Then in the close_app() write this

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

It would be a good idea to update your phonegap/cordova version to the latest build and use the code provided above.

Upvotes: 1

Related Questions