Remember ajax added data when hitting back button

You have a few options.

  1. Use localstorage to remember the last query
  2. Use cookies (but don’t)
  3. Use the hash as you tried with document.location.hash = "last search" to update the url. You would look at the hash again and if it is set then do another ajax to populate the data. If you had done localstorage then you could just cache the last ajax request.

I would go with the localstorage and the hash solution because that’s what some websites do. You can also copy and paste a URL and it will just load the same query. This is pretty nice and I would say very accessible.

I am curious to know why it didn’t work. Update your answer so we can help.

Leave a Comment