2

I've tried doing some research on the various jquery history plugins, but I can't find any examples for my situation, so I'm starting to think maybe what I'm trying to do is not possible.

We have a very complicated search page that updates with ajax. Users search using a ton of options, and they get back a list of results which they can sort, page etc. Then if they click on one of the results, it navigates them to another page to view the details. However, if they click Back they do not return back to how the page appeared after all the ajax and javascript updates. They see the search page with none of their results.

I was hoping that I could pull of something with adding a hash before they navigated away, or using one of the jquery history plugins to achieve something similar, so that when they clicked Back, it wouldn't RELOAD the search page, but would just show them their cached version (how it last looked when they clicked on one of the results).

From what I've seen, it looks like most of the examples I've found for ajax and back buttons use a hash value that tells the page how to arrange itself, even allowing for bookmarking the page that includes the hash. I think for me that would mean that I'd basically have to serialize everything in the search page into a hash value, which doesn't seem practical unless I am totally misunderstanding how it works.

Does anyone out there know if this possible?

chrismay
  • 1,384
  • 2
  • 15
  • 25

3 Answers3

4

There are at least 2 ways to do what you want:

  1. "Classic" - store all user search options in cookie or in session, like "last search". So, when user navigates to search page during session, you can read cookie / session and show last search results with that options.
  2. "Modern" way - use HTML5 history API - on each search form a search options object and push it in via history.pushState - when user navigate to other page and then presses "back", browser will use this state to perform a search.
S Korolev
  • 346
  • 1
  • 6
  • Ok so it looks like there is now way for the page to just automatically display how it appeared when the user left it is that right? Either way I'll have to user JS to save the entire state of the page into some container and then reconstruct the page when the user returns? – chrismay Jan 28 '14 at 17:27
  • Yes, browser itself don't "remember" ajax requests in history, unlike normal URLs. You can only emulate it for user in a several ways. – S Korolev Jan 30 '14 at 09:14
1

If it's that complex, better you develop your own solution without any plugin. Just use location.hash to get and set hash value and store all form input elements and their values after hash like a querystring input1=a&input2=b

On every form submit update hash querystring

If user navigates back in history read hash value parse it and update your form fields and submit to get search results automatically.

Ergec
  • 11,608
  • 7
  • 52
  • 62
0

you can check out SammyJS this is the plugin I used for ajax history. Hope it helps

johnpili
  • 688
  • 6
  • 9