javascript - Cordova navigator.app.backHistory button on html different approach -


i'm building hybrid app intel xdk , need button , it's function. have 1 index.html file. "pages" 's , each 1 have different id. navigate through them using activate_subpage("#uib_page_10");

 $(document).on("click", ".firs_div_button", function(evt){      //#uib_page_10 div it's content      activate_subpage("#uib_page_10");    var thispage = 1;  gobackfunction (thispage);  //call function , pass page number    });  $(document).on("click", ".second_div_button", function(evt){      //#uib_page_20 div it's content      activate_subpage("#uib_page_20");  var thispage = 2;  gobackfunction (thispage);  //call function , pass page number        }); 

i have set eventlistener hardware on button.

document.addeventlistener("backbutton", onbackkeydown, false);  function onbackkeydown() { alert("hello"); navigator.app.backhistory(); } 

this functional not work should, in case , app. when navigate 1 page (5 pages / divs) , hit button, not go first page. go "back" history deep , close app, without changing actual page (view) before closing.

now, have idea, need this. not use history back, use counter , dynamic array 5 elements.

 function gobackfunction (getactivepage) {  var active_page = getactivepage;  var counter = 0; // init counter (max 5)  var history_list = [counter][active_page];  // empty array  counter = counter + 1;   :  :  :  }  document.addeventlistener("backbutton", onbackkeydown, false);  function onbackkeydown() {  //read array , it's positions activate:  activate_subpage("#page_from_array");   counter = counter - 1;  if (counter == 0) {   //trigger app exit when counter get's 0.   navigator.app.exitapp();  } } 

this idea, not tested. store list of opened pages in array , when button pressed, activate pages taken array list, backwards.

i not know how this, i'm not expert :( there may batter way this. if have suggestion, accept :d

i save array in localstorage pages navigated , go using pop() on array. @ moment, it's best way got go back. code:

// first, create table "pages"

function init_pages_table() {     var pages = new localstoragedb("pages", localstorage);     if (!pages.isnew())      {         pages.drop();         pages.commit();     }     var pages = new localstoragedb("pages", localstorage);     pages.createtable("pages", ["nome"]);     // commit database localstorage     // create/drop/insert/update/delete operations should committed     pages.commit(); } 

// add page array:

function push_pagename(pagename) {     var pages = new localstoragedb("pages", localstorage);     if (!pages.tableexists("pages"))     {         init_pages_table();         pages = new localstoragedb("pages", localstorage);     }     pages.insert("pages", {nome: pagename});     pages.commit(); } 

// pop page form array:

function pop_pagename() {     var output = '';     var id_page = ''     var pages = new localstoragedb("pages", localstorage);             var last_page = pages.queryall("pages", { limit: 1,                         sort: [["id", "desc"]]                       });     $.each(last_page, function(index,value){                 output = value.nome;                 id_page = value.id;                 return false;             });            var rowdeleted = pages.deleterows("pages", {id: id_page});     pages.commit();     return output; } 

you can define functions set, get, read:

function set_backpage(pageurl) {     push_pagename(pageurl); } function get_backpage() {     return pop_pagename(); } function read_backpage() {     var output = '';     var id_page = ''     var pages = new localstoragedb("pages", localstorage);             var last_page = pages.queryall("pages", { limit: 1,                         sort: [["id", "desc"]]                       });     $.each(last_page, function(index,value){                 output = value.nome;                 id_page = value.id;                 return false;             });      return output; }