2

I have problem with browser back button. Problem exists for IE and google chrome. I'm creating autoload mechanism for search engine. Mechanism works like google search.

Procedure of building:
1) typing keywords
2) ajax search request
3) response as json
4) building results list using json
5) append list to container

If i have builded results and redirect to another page and back to the page with results, results desapear. I tried a lot solutions described by developers like hashing, history.js and many more but every one is not working.

  • Try describing in detail (at least for one thing you tried) what exactly have you tried (with code snippets or better yet a live example), and the exact way it failed. Otherwise it's hard to give you a useful answer. – Nickolay Oct 21 '11 at 22:58

1 Answers1

0

When you go back, the original HTML of the page is loaded from the cache. Everything you added through Javascript is not stored, so you will have to restore that modification on page load.

For that you can use the popstate event. When that even is fired, you'll need to restore the information. You can do that by re-executing the AJAX request and process the result. Therefore you must save enough information in the url (or hash) to be able to execute the same request again.

That also means, you may need to do earlier requests! For example, if you execute an ajax request to get item X of a list, where X increments each time after the request (so you can get the next item on each click), you will need to make sure that you load all items again. If you don't do that, you will only get the original items on the cached page, and the latest item that was AJAXed, while the items inbetween will be missing.

But if you use pushState or replaceState to store states, you can also store additional data. You can use this to store the JSON result with the state, so you don't need an additional request. Anyway, this is an optimization and is not strictly needed, so you should start with implementing the AJAX request being fired on popstate. You'll need that anyway, because the data may not always be stored with the state, so you will always need the AJAX request as a fallback.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Cool advice, but (starting from the 2nd paragraph) not quite relevant to out of date browsers the OP is having problems with, though... – Nickolay Oct 21 '11 at 22:56
  • @GolezTroll: That's not accurate, take a look here: http://stackoverflow.com/questions/1195440/ajax-back-button-and-dom-updates – Tomasz Zieliński Aug 08 '12 at 23:39