Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbutton in phonegap not getting registered and not working properly

I am using phonegapand i am registering backbutton onDeviceReady Function but getting function called when clicked on device's back button. and i have added

<script src="lib/cordova-2.6.0.js"></script>

document.addEventListener("backbutton", onBackClickEvent, false);

function onBackClickEvent() {
    alert("back onBackClickEvent");     
}

This onBackClickEvent() function is not getting called, I've never seen that alert poped up. I am also getting an error of Uncaught ReferenceError: cordova is not defined

what could be the error Please suggest me. Thanks in advance.

like image 238
Deepika Lalra Avatar asked Jun 14 '13 04:06

Deepika Lalra


2 Answers

Don't forget to call the "deviceready" event.

From phonegap doc :

This is a very important event that every Cordova application should use.

Cordova consists of two code bases: native and JavaScript. While the native code is loading, a custom loading image is displayed. However, JavaScript is only loaded once the DOM loads. This means your web application could, potentially, call a Cordova JavaScript function before it is loaded.

The Cordova deviceready event fires once Cordova has fully loaded. After the device has fired, you can safely make calls to Cordova function.

Try this

document.addEventListener("deviceready", onDeviceReady, false);

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

function onBackClickEvent()
{
    alert("back onBackClickEvent");     
}
like image 56
gaepi Avatar answered Nov 10 '22 07:11

gaepi


I got the answer to my Question. I was struggling and many developers tries to resolve it and when i found the silly mistake i found myself so silly.

I was writing

document.addEventListener("deviceready", onDeviceReady(), false);

instead of

document.addEventListener("deviceready", onDeviceReady, false);

thats why i am getting an error cordova is not defined and no other listeners are getting registered.

like image 4
Deepika Lalra Avatar answered Nov 10 '22 06:11

Deepika Lalra