Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Mobile - How to get the data-url in the pagebeforechange event

I would like to get the data-url of the page that triggered the page change. Any ideas?

$(document).bind("pagebeforechange", function ( event , data) {
    // get the data-url of the link that triggered the page change
});
like image 489
bmurmistro Avatar asked Feb 23 '12 16:02

bmurmistro


2 Answers

You can get the page from which the user is coming within the data object:

$(document).bind("pagebeforechange", function ( event , data ) {
    console.log(data.options.fromPage.attr('data-url'));
});​

Note that since you are binding to the document element rather than a single data-role="page" element that this will fire twice for each page-change, once for the fromPage and once for the toPage. The data passed into the event handler for each page's event will be the same as the data for the other page.

Here is a demo: http://jsfiddle.net/Atfrf/2/

like image 188
Jasper Avatar answered Nov 14 '22 22:11

Jasper


The attr method is not work at some browsers, so you'd better to use the jqmData method. The jqmData can work at any namespace.

$(document).bind("pagebeforechange", function ( event , data ) { console.log(data.toPage.jqmData('url')); });​

like image 41
Takayuki MINATO Avatar answered Nov 14 '22 23:11

Takayuki MINATO