views:

177

answers:

2

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).

A: 

// 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.

Espen
Also 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
A: 

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)

Jake יעקב Wolpert