JANNURM
JANNURM

Reputation: 331

Controlling back button using PhoneGap

I have a index page and many button on that index page. Clicking those button page will go to some other pages. I have this code on index.htlm

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

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown() {
    alert("Are you sure !!")
}

<body onLoad="onLoad()"> </body>

So when I am in the index page back button should only give that alert. I have no problem with that, the alert is showing properly but back button is disabled from other page also.

Upvotes: 0

Views: 960

Answers (4)

user
user

Reputation: 348

Try adding after your alert:

document.removeEventListener("backbutton", onBackKeyDown, false);

Upvotes: 0

WooDzu
WooDzu

Reputation: 4866

  1. Download more recent cordova-2.0.0.js
  2. Place it in your root directory (next to index.html)
  3. Load it in your <head> section
<head>
     <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js">
     <!-- Other JS files .... -->
</head>

Upvotes: 1

mornaner
mornaner

Reputation: 2424

You are not checking the page in your function, so it reacts equally for every page. Try this:

function onBackKeyDown() {
    if($.mobile.activePage.is('#homepage')){
       e.preventDefault();
       alert("Are you sure !!");
   }
   else {
       navigator.app.backHistory()
   }
}

Upvotes: 1

Piotr Krysiak
Piotr Krysiak

Reputation: 2815

Try to put your script inside page divs. This may help.

Upvotes: 1

Related Questions