Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile: Getting ID of previous page

I basically need a custom function to be used only when, for example, the #reviews page is clicked on from the home page.

Here is the current code I am using:

$(document).bind("mobileinit", function(){
    $('#reviews').live('pagebeforeshow',function(event){
    var cpage = $.mobile.activePage.attr('id');
        if (cpage == 'home') {
            addPages();
        }
    });
});

The problem is, even though it is using 'pagebeforeshow' it is using the id of the page it is about to transition to as the active page, in this case 'reviews' rather than home, thus causing the if statement to fail.

So my question is, in this example, how would i get the id of the previous page, to make sure it is in fact coming from the #home page?

like image 859
tblakey89 Avatar asked Oct 02 '11 19:10

tblakey89


2 Answers

To complete the answer: you will get a jQuery object back, so to get the ID of the page do this:

$("#page-id").live('pagebeforeshow', function(event, data) {
    console.log("the previous page was: " + data.prevPage.attr('id'));
});

data.prevPage is getting the previous page as jQuery object,

.attr('id') is getting the id from that jQuery object.

like image 124
Michael Trouw Avatar answered Oct 13 '22 02:10

Michael Trouw


pagebeforeshow event handler actually gets 2 params: event and ui. You can access the previous page from the ui. This is an example from jquery-mobile event docs:

$('div').live('pageshow', function(event, ui){
  alert( 'This page was just hidden: '+ ui.prevPage);
});

Those params are the same for all 4 page show/hide events.

like image 20
mkriheli Avatar answered Oct 13 '22 01:10

mkriheli