lawls
lawls

Reputation: 1508

Tell when a user is going back in browser history

I have a page A that displays some text from my database. The text is editable and gets autosaved using AJAX. If the user would go away from that page, and then go back to page A using browsers history functionality, the page would not have the latest data (since we went back in history). And the user would edit the old data, which would overwrite the latest data on the server when it gets autosaved.

I assume this is purely a front-end issue, where my server can do nothing about this. What solutions could be aplied? If it was possible do detect with javascript that the user went back in history, then I could simply display a text saying that the user has to refresh the page. But is that even possible? Or are there any better solutions?

Upvotes: 1

Views: 148

Answers (2)

drew_w
drew_w

Reputation: 10430

There are lots of options and strategies for a situation like this.

  • The first thing you can do is to try to disable caching on the page. You can use meta tags to do this.

  • You can also keep track of when the user presses the back button using libraries such as this one. You can respond either on the server or on the client, although you want to be careful because a disabled back button can annoy users.

  • Should you ever happen to consider using a javascript framework such as AngularJS you can probably keep track of the back button using the framework.

  • Finally you can solve issues like this with careful page design. If the data on a page can change you might load the current data via ajax before the user has a chance to edit it. By doing this - your "load" code will run even if the user does use the back button. Take a look at this stack for more information on that!

Hope these suggestions help a bit!

Upvotes: 4

ajitksharma
ajitksharma

Reputation: 2019

If you are using Jquery then use/

$(document).on('pageshow', '#Content' ,function() 

in place of

$(document).ready(function()

It will solve your problem, the javascript file that is back end will be loaded when that particular page loads

Upvotes: 0

Related Questions