I don't use the Cordova, but this code might help you:
String . prototype . isEmpty = function () { return this . length === 0; };
var browserHistory;
function hash ( name, historyChange )
{
"use strict";
if ( name !== undefined )
{
if ( hash () !== name )
{
if ( ! historyChange ) { browserHistory . add ( name ); }
window . location . hash = name;
}
}
else
{
var newHash = window . location . hash . split ( "#" ) [ 1 ];
return newHash === undefined ? "" : newHash;
}
}
browserHistory =
{
currentIndex : 0,
history : [],
add : function ( name )
{
"use strict";
this . currentIndex = this . currentIndex + 1;
if ( this . history . length - 1 > this . currentIndex ) { this . history . splice ( this . currentIndex ); }
this . history . push ( name );
},
backward : function ()
{
"use strict";
if ( this . currentIndex === 0 ) { return; }
this . currentIndex = this . currentIndex - 1;
this . changeHash ();
},
changeHash : function ()
{
"use strict";
hash ( this . history [ this . currentIndex ], true );
},
forward : function ()
{
"use strict";
if ( this . currentIndex === this . history . length - 1 ) { return; }
this . currentIndex = this . currentIndex + 1;
this . changeHash ();
},
init : function ( name )
{
"use strict";
this . history . push ( name );
}
};
window . onhashchange = function ()
{
"use strict";
if ( hash () . isEmpty () ) { load ( { name: "" } ); }
else { load ( { name: hash () } ); }
};
window . onload = function ()
{
"use strict";
if ( ! hash () . isEmpty () )
{
browserHistory . init ( hash () );
load ( { name: hash () } );
}
else { browserHistory . init ( "" ); }
};
This script:
1) Work with sites that uses hashes ( http(s)://(www.)name.domain/#urlHash ).
Examle:
https://www.example.com/page - default url
https://www.example.com/page#login - login subpage
https://www.example.com/page#images - images subpage
etc.
2) "window . onload" function check if entered url contain a hash and init browserHistory with it or with: "" ( empty string ) - no hash is my main page
3) "browserHistory" object save a page history
4) "hash" function called without arguments returns current url hash and with "name" parameter change url hash
5) When hash is changed "window . onhashchange" function is called
6) "window . onhashchange" function check hash
If the url does not contain hash, script load the default page.
If the url contain a hash, script load a subpage based on hash.
7) In function "load" ( not described here ) I have an XMLHttpRequest, that call php file with name argument and set to main div element html code that was returned from that php file.
8) Instead of ( for example ):
<a class="a_nice_style" href="https://www.example.com/images.html>Images gallery</a>
You can use ( for examle ):
<p class="a_nice_style" onclick="hash ( 'images' );">Images gallery</p>
9) Function named: "load" have an object as a parameter. This object is send to php file ( as an "$_GET" or "$_POST" array ), so you can call "load" function with custom object that contains for example login fields values.
10) "browserHistory" have only hashes of your page.
This will be your button onclick function:
onBackButton: function() { browserHistory . backward (); }
You can modify a "browserHistory . backward" function to exit your app by replace: "return;" in this line:
if ( this . currentIndex === 0 ) { return; }
with your application exit code.