I have an iPhone webapp that uses a cache manifest to work offline. I add the webapp to my home screen, use it (say scroll to a certain location on a page), then go back to homescreen. When I open the app again, for a brief moment I see where I used to be (at that scrolled location on that page), but then the app "reloads" and I get scrolled to the top of the mainpage. Is there a way to prevent this "reloading"? This happens even in airplane mode (ie everything is working off the cache).
-
This always bothered me in the normal Safari app, it always tries to reload the previous page... – Fosco Jul 19 '10 at 18:30
4 Answers
You're just seeing the default startup image, which is just a screenshot of the last place you were at. It's not "reloading"; the app wasn't loaded to begin with.
Search for "apple-touch-startup-image" to set a real loading image.

- 55,829
- 10
- 121
- 131
-
Ah, so it's an illusion that the state I left the app in has been remembered? No chance of actually storing the scroll location right before closing? – Alec Jacobson Dec 01 '10 at 17:54
-
You might be able to save it to localStorage yourself: when you get onscroll, reset a 250ms timer; when the timer expires, save the scroll position, maybe relative to the nearest anchor to account for the page changing. Restore it on load. Only do any of this when window.navigator.standalone is true. – Glenn Maynard Dec 01 '10 at 19:07
What I'm struggling with here is that the app actually seems to stay "in memory" longer if I use regular Safari as opposed to running in "apple-mobile-web-app-capable" mode. In the later case something as simple as pressing the home button, then task-switching back to the app causes a reload. Doing the same thing just in Safari often does not reload. So I'm worse off by using "apple-mobile-web-app-capable".

- 440
- 4
- 8
I don't believe there is a real 'reload' event. onload and onunload are all we get.
the onload handler starts up as if it is your first time coming to the page.
the onunload handler is the key to clearing out old content.
I like to provide alternate content for people who are coming back to my web app.
window.onunload=function(){
document.getElementsByTagName('body')[0].className+=' unloading'
}
And let the CSS do the dirty work to hide most of the body and show alternate content.
(this answer does not rely on jQuery or other frameworks)

- 603
- 6
- 12
// on load
window.scroll(0,0);
To ensure no old content is displayed while launching I use this in my page:
window.addEventListener('unload', function() { $('body').hide(); } );
Thus the last state of the page is empty and is what is shown to the user when the page is opened again.

- 2,133
- 4
- 19
- 19
-
1Also a good idea to ensure the app is using the latest files from the cache manifest: window.applicationCache.addEventListener( 'updateready', function(){ window.applicationCache.swapCache(); window.location.reload(true); }, false ); – Espen Oct 03 '10 at 14:53