
Work-around for jQuery Mobile issue #4050
By: a guest on
May 23rd, 2012 | syntax:
JavaScript | size: 1.65 KB | hits: 18 | expires: Never
$(document).on('pagebeforechange', function(event, data) {
// pagebeforechange fires both before and after the request; data.toPage is only a string before
if ($.mobile.ajaxEnabled && typeof data.toPage === 'string') {
/* NOTE: $cur_page may be undefined if reloading a page w/ hash in browser w/o pushState support
$initial_page may be undefined, empty, or reference an unenhanced page */
var $cur_page = $.mobile.activePage,
$initial_page = $cur_page && $cur_page.jqmData('external-page')
? $cur_page.siblings(':jqmData(role="page")').not(':jqmData(external-page="true"))
: $cur_page,
initial_page_data = $initial_page && $initial_page.jqmData('page'),
root_url = window.location.protocol + '//' + window.location.host,
nav_urls = {
dest: $.mobile.path.makeUrlAbsolute(data.toPage, root_url),
initial: $initial_page && $initial_page.length ? root_url + $initial_page.jqmData('url') : undefined
};
// for when initial pg data-url & return link disagree on trailing "/"...
$.each(nav_urls, function(key, value) {
nav_urls[key] = value && value.charAt(value.length - 1) === '/' ? value.slice(0, -1) : value;
});
if ($initial_page !== $cur_page
&& nav_urls.dest === nav_urls.initial
&& !(initial_page_data && initial_page_data.options.domCache)) {
data.options.reloadPage = true;
$(document).one('pageshow', function() {
// keep new version of the page around (is this necessary?)
$(event.target).removeAttr('data-' + $.mobile.ns + 'external-page').off('pagehide.remove');
// clean up dup DOM node from any trailing slash disagreement
$initial_page.remove();
});
}
}
});