I have a simple jQuery Mobile contact-list application. It offers the users an option to add it to their home-screen. When they do, clicking a phone number to start a call and opening the application again puts them to the first page of the app. To prevent this behavior, I added the following:
$(function () {
//if I am at the start page
if ($.mobile.activePage.attr('data-url') === '/') {
var storedPage = localStorage.getItem('jqmPage');
//and I have a stored link
if (storedPage !== null) {
//change the page to the stored link
$.mobile.changePage(storedPage);
}
}
$(document).bind('pagecreate', function (e) {
var url = !!e
? $(e.target).attr("data-url")
: location.pathname + location.search;
//there can be dialog pages - we don't want to return to them
if (url.indexOf('/') !== 0) {
return;
}
localStorage.setItem('jqmPage', url);
});
});
However, when the users returns to the application, the page cache is lost and all the data on that page, which normally should stay open, is re-loaded from the server (see $.mobile.changePage(storedPage)). Is there any way to prevent this? Is there also any elegant way of achieving the same effect? Should I just store active page HTML on the localStorage as well? If so, how do I reactivate it?
Thanks for the help.
edit: Maybe I wasn't clear enough. I can already store data on the localStorage. Using cookies to store pieces of HTML that would be transfered to the server on each request would be simply ridiculous, as far as I can tell. My question isn't about how to store data. What I am really asking is, by simply storing $(".active-page-class").html() and putting it back on the page when it is not there, I want to imitate a client side caching which iPhone does not provide on ajax based jQuery mobile applications. This is about jQuery Mobile for the apps that are accessed via iPhone homescreen, as the tags and the question already states.