Phonegap – not working on HTML back button

In my app i am using phonegap 2.6.For back button, I am using the following function

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

function onBackKeyDown() {

document.addEventListener('deviceready', onDeviceReady, true);

The above function works fine when I click on the device’s hardware back button. But when I click on the back button it is not working.

I have designed my back button as below:

<a class="ui-link" href="#" rel="nofollow noreferrer noopener" rel="external" onclick="onBackKeyDown()">
        <img src="images/icon-back.png" alt="Phone" border="0">

But this button is working fine for this; (application exit).

//Working Fine
function onBackKeyDown() {;

//Not Working
function onBackKeyDown() {;

but not working for;.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I have tried 3 separate things when I faced the same situation:

  • window.history.back()


  • History.go(-1);

Individually, none of these solve the problem. I put all 3 things together and much to my surprise it worked. I really don’t know what is behind it.

Then I decreased to two functions and removed:

  • window.history.back()

Now I am using this function and it is working fine.

//Works Fine
function onBackKeyDown() {

Solution 2

If you use the attribute data-rel=”back” on an anchor, any clicks on that anchor will mimic the back button, going back one history entry and ignoring the anchor’s default href.

Solution 3

it depends where you are:
on my windowsphone 8.1 lumia 925, it works history.go(-1);,
while; causes an exception before crashing.

On my Android (I believe the majority),; works properly.

Solution 4

This might help some people, as it helped me fix the history.go(-1) not working in google chrome browser.

// Doesn't work in Chrome browser
function onBackKeyDown() {
// Does work in Chrome browser
function onBackKeyDown() {
    return false; //needed in chrome to prevent about:blank page.;
    return false; //needed in chrome to prevent about:blank page.

Im no expert at all in jQuery, but thought it might help someone, as I was looking for this answer but couldn’t find it at first.

Solution 5

Solved! Stop getting “TypeError: is undefined”

A function I created that will first check what device you’re on and then apply the relevant script:

function onBackKeyDown() {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
        // IOS DEVICE
    } else if (userAgent.match(/Android/i)) {
        // ANDROID DEVICE;
    } else {

Call function by adding this to your back link/button:


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply