Skip to content

Instantly share code, notes, and snippets.

@adammessinger
Created May 24, 2012 03:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adammessinger/2779268 to your computer and use it in GitHub Desktop.
Save adammessinger/2779268 to your computer and use it in GitHub Desktop.
jQuery Mobile Work-Around: refresh cached first page
// guarantee fresh data on the first-loaded page (stays cached in DOM)
// see https://github.com/jquery/jquery-mobile/issues/4050
$(document).on('pagebeforechange', function(event, data) {
var $all_pages = $.mobile.pageContainer.find(':jqmData(role="page")'),
$cur_page = $.mobile.activePage,
$first_page, // unenhanced if fresh load of URL w/ hash in browser w/o pushSate support
first_page_data,
toPage_type,
root_url,
nav_urls;
// exit early if fix is unnecessary
if (!$.mobile.ajaxEnabled // no ajax nav, so no problem to fix...
// ...or a fresh non-ajax page load...
|| $cur_page === undefined
// ...or a multi-page document
|| $all_pages.length > 1 && !$all_pages.filter(':jqmData(external-page="true")').length) {
return;
}
toPage_type = typeof data.toPage;
// pagebeforechange fires both before and after the request; data.toPage is
// only a string before. BUT, in IE when using the back button to return to
// the first page it's only ever a jQuery collection so also run on hash changes.
if (toPage_type === 'string' || data.options.fromHashChange) {
$first_page = $.mobile.firstPage;
first_page_data = $first_page.jqmData('page');
root_url = window.location.protocol + '//' + window.location.host;
nav_urls = {
dest: $.mobile.path.makeUrlAbsolute(toPage_type === 'string'
? data.toPage
: data.toPage.jqmData('url'), root_url),
first: root_url + $first_page.jqmData('url')
};
// for when initial pg data-url & return link disagree on trailing "/"...
$.each(nav_urls, function(key, value) {
nav_urls[key] = value.charAt(value.length - 1) === '/' ? value.slice(0, -1) : value;
});
if ($first_page !== $cur_page
&& nav_urls.dest === nav_urls.first
&& !(first_page_data && first_page_data.options.domCache)) {
// work around Android Browser giving blank or stale page w/ back button use, IE 8 & 9 giving stale page
if (data.options.fromHashChange) {
window.location.replace(nav_urls.first);
return;
}
data.options.reloadPage = true;
$(document).one('pageshow', function(event) {
var $new_first_page = $(event.target);
// clean up dup DOM node from any trailing slash disagreement
$first_page.remove();
// keep new version of the page around
$new_first_page.removeAttr('data-' + $.mobile.ns + 'external-page').off('pagehide.remove');
$.mobile.firstPage = $new_first_page;
});
}
}
});
@taivn07
Copy link

taivn07 commented Jan 6, 2013

not working anymore for jQM 1.2.0.

@renhart
Copy link

renhart commented Jan 22, 2013

Seems to work for me in jQM 1.2.0 after commenting out lines 38-40. Those lines remove the trailing slash in IE 9.

@vijayrudraraju
Copy link

the best way in my opinion (as long as you don't need to cache any page) is to find the line in 'jquery.mobile-1.2.0.js' that reads:

$.mobile.loadPage.defaults = {....blah};

and

change the 'reloadPage' default to 'true'

@diosney
Copy link

diosney commented Feb 24, 2013

Doesn't work on 1.3 neither.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment