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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With