-
-
Save adammessinger/2779268 to your computer and use it in GitHub Desktop.
// 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; | |
}); | |
} | |
} | |
}); |
I'm unable to get this to work. Where should I be placing it in my pages?
Never mind - I got it working. Thanks for this!!
I have it in an external script file, along with the other custom script for the app I've been working on. This file is loaded after jQuery, jQuery Mobile, and any plugins.
Oops, posted my reply on an old version of this page and didn't see that you already had it working.
Thanks for this!!
You're welcome! Let me know if you have trouble.
Thanks!!
Note that the this script breaks down if the URLs that dont contain a document and doesnt end with an '/'.
This is stated as mandatory in the jquery docs though but thought it could be good for people to know.
And thanks alot for the script saved me so much (more) head-ache !
So I am just useless, disregard comment above, problem obvisouly was I needed to upgrade from 1.6.4 to 1.7 !
So just upgraded from JQM 1.1.1 to 1.2.0 and the fix doesnt work anymore. Anyone else seeing this problem?
It seems to break down when I return to the "initial page" through link on my page, but still works if i return to the initial-page with the browsers back-button
not working anymore for jQM 1.2.0.
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.
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'
Doesn't work on 1.3 neither.
Work-around to address jQuery Mobile issue #4050: jquery-archive/jquery-mobile#4050
Tested in the following browsers...
Desktop Test Browsers
Mobile Test Browsers
Tested using an emulator unless a device is given in (parens).