Navigation Menu

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;
});
}
}
});
@adammessinger
Copy link
Author

Work-around to address jQuery Mobile issue #4050: jquery-archive/jquery-mobile#4050

Tested in the following browsers...

Desktop Test Browsers

  • IE 9 (Windwows 7)
  • IE 8 (Windows XP)
  • FF 12.0 (Windows 7)
  • FF 11.0 (Windows XP)
  • FF 10.0.4 ESR (Windows XP)
  • Safari 5.1.7 (Windows 7)
  • Safari 5.0.5 (Windows XP)
  • Safari 4.0.5 (Windows XP)
  • Chrome 19 (Windows 7)
  • Opera 11.64 (Windows 7)

Mobile Test Browsers

Tested using an emulator unless a device is given in (parens).

  • Mobile Safari for iOS 5.1.1 (4th Gen iPod)
  • Android Browser for 4.0.2 (Galaxy Nexus)
  • Android Browser for 3.2
  • Android Browser for 2.3.4 (HTC Droid Incredible)
  • Android Browser for 2.3.3
  • Android Browser for 2.2
  • Firefox for Android 10.0.4 (HTC Droid Incredible)
  • Opera Mobile 12 (HTC Droid Incredible)
  • Chrome for Android Beta 0.18 (Galaxy Nexus)
  • Windows Phone 7.5

@j-pdx
Copy link

j-pdx commented Jun 27, 2012

I'm unable to get this to work. Where should I be placing it in my pages?

@j-pdx
Copy link

j-pdx commented Jun 27, 2012

Never mind - I got it working. Thanks for this!!

@adammessinger
Copy link
Author

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.

@adammessinger
Copy link
Author

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.

@joenivl
Copy link

joenivl commented Aug 10, 2012

Thanks!!

@taracus
Copy link

taracus commented Aug 31, 2012

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 !

@taracus
Copy link

taracus commented Aug 31, 2012

So I am just useless, disregard comment above, problem obvisouly was I needed to upgrade from 1.6.4 to 1.7 !

@taracus
Copy link

taracus commented Oct 18, 2012

So just upgraded from JQM 1.1.1 to 1.2.0 and the fix doesnt work anymore. Anyone else seeing this problem?

@taracus
Copy link

taracus commented Oct 18, 2012

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

@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