Wednesday, March 21, 2012

ajax and browser refresh function

When a web app is 'ajaxified' to the extreme the browser accesses the app with a GET http request and from this moment on all interaction with the server is done through ajax.

The question I have now is how to deal with the browser refresh button. if the user presses it, all what the server is going to see is the initial GET request, which means that I am not getting viewstate or any other state variables, which in many cases is bound to break the application.

What do you think is the right way to deal with this problem? I do not really need this standard refresh functionality and would be glad to disable it, but I know of no reliable way of doing this.

Any thoughts?

I can think of two ways to handle this...none of which are optimal. One, you can put some sort of message warning the user what will happen if they use the refresh button. I can see using this temporarily or if you app is an in-house app. Two, you could use good ol' session to keep the user data on the back end. If the user hits refresh, you will have to build your page from that session data...so it basically won't matter if they hit refresh.


My app is a business app. Still, I can not expect that the refresh button will not be pressed. I need to handle it gracefully.

It looks like for now my definition of 'gracefully' will mean that I will

a) disable as many ways to cause a refresh as I can by intercepting keypresses with javascript, disabling popup menus, hiding the standard toolbar

b) if refresh makes it through my defences I will treat it as a new session.


I use the Really Simple History framework:

http://code.google.com/p/reallysimplehistory/

Ofcourse, you won't be able to use it with anything other than customjavascript .asmx calls. Leveraging Microsoft's Ajax Toolkit'scontrols you can't use anything liek that; well, you pretty much can'tdo ANY custom javascript.

Just something to look into.


This is cool stuff. We stole their idea and used a simplified variation of it to block the back button. Unfortunately it does not cover the refresh button. As to "can't do ANY custom javascript" I consider this to be a good thing - I do not trust my application programmers enough to give them free reign to do "ANY custom javascript". I hope that doing javascript within confines of the Microsoft ajax framework will give me better control over how the javascript is done.

Speaking of controlling the browser history - did you see thishttp://www.nikhilk.net/BackButtonSupport.aspx ?


Check out the ASP.NET Futures: http://www.asp.net/downloads/futures/, it has some support for managing browser history (http://quickstarts.asp.net/Futures/ajax/doc/history.aspx).

All that said, I don't think it guards against a refresh, but I could mistaken.

You could also try this technique...http://www.pascarello.com/rememberHistory.aspx

-Damien

No comments:

Post a Comment