Friday, October 28th, 2005
Ajax: Tackle the Refresh Button
<>p>Eric Pascarello is tackling the browser refresh and its effects on Ajax applications.The last thing we want is for a user to hit refresh and all of your nice state goes bye-bye.
Eric takes an approach that changes the URL between state changes, allowing a refresh to capture the info:
function makeRequest2(str){ var loader1 = new net.ContentLoader("testHistory.aspx",finishRequest2,null,"POST","q=" + str); window.location.hash = str; } function finishRequest2(){ document.getElementById("spanId2").innerHTML = this.req.responseText; } window.onload=function(){ if(window.location.hash.length > 0) makeRequest2(window.location.hash.substring(1,window.location.hash.length)); }The first thing is we set the hash of the window to the parameter that the server recognizes. (Note: setting the hash does not cause the page to refresh/submit!) If your system is a little more advanced, you will have to figure out a relationship between the server and the client. The other thing we do is add the onload handler to the document so we can look for the hash. If it exists, we remove the # and send the XMLHttpRequest to the server. The request then fills in the information on the page. The user may notice the data swap depending on the lenght of time for the response. It is a rather simple solution with a lot of work that needs to be done if you have a complex request.
Related Content:











This has been my single biggest frustration with my one-week switch from Safari to Firefox – Firefox forgets state, while Safari “just works”.
I’ve tried experimenting with the location hash in javascript and the one quirk I found was that there’s no browser event I could find to capture if the user hits the back/forward buttons and navigates between different hashes.
Just use hidden form fields for state.
Always keep all state there if it needs to be remembered. It works great in my experience so far (across simple refresh, backward, and forward).
My only comment to the textbox solution is: But can you bookmark a textbox? You can bookmark the hash.
From what I understand the theory is this:
You specify a hash each time a function is called, and then that automatically works right? We just need to make a random hash for each call. Am i Right ?
what kind of programing need
More advanced and detailed article you can find right here:
http://www.contentwithstyle.co.uk/Articles/38/fixing-the-back-button-and-enabling-bookmarking-for-ajax-apps