Reputation: 1606
I'm using this code here to as router to handle pages in single page application ( Cordova ).
var router = (function () {
"use strict";
var routes = [];
function addRoute(route, handler) {
routes.push({parts: route.split('/'), handler: handler});
}
function load(route) {
window.location.hash = route;
}
function start() {
var path = window.location.hash.substr(1),
parts = path.split('/'),
partsLength = parts.length;
for (var i = 0; i < routes.length; i++) {
var route = routes[i];
if (route.parts.length === partsLength) {
var params = [];
for (var j = 0; j < partsLength; j++) {
if (route.parts[j].substr(0, 1) === ':') {
params.push(parts[j]);
} else if (route.parts[j] !== parts[j]) {
break;
}
}
if (j === partsLength) {
route.handler.apply(undefined, params);
return;
}
}
}
}
$(window).on('hashchange', start);
return {
addRoute: addRoute,
load: load,
start: start
};
}());
and this router is used like this:
router.addRoute('', function() {
document.getElementById("bodyDiv").innerHTML = homePageHtml;
});
router.addRoute('page1', function() {
document.getElementById("bodyDiv").innerHTML = page1Html;
});
i want to add back button to my application but it didn't work. i did try
window.history.back();
history.go(-1);
navigator.app.backHistory();
but none of them worked the way it suppose to work. when i open the application on google chrome, it working just fine but when i convert it to mobile application using Cordova the back button doesn't work as expected sometimes the back button instead of taking you to the previous page it refresh the current one.
Upvotes: 0
Views: 515
Reputation: 367
You can use this:
This is only an example for backbutton press.
document.addEventListener("backbutton", function () {
exitAppPopup();
}, false);
function exitAppPopup() {
navigator.app.exitApp();
}
Upvotes: 1
Reputation: 1123
Do you have tried something like this?
document.addEventListener("backbutton", back_function, false);
function back_function() {
// Handle event
}
Upvotes: 0