machineghost
machineghost

Reputation: 35790

Is there any Backbone-friendly way to detect back button presses?

Stack Overflow has several questions related to detecting back button presses, the most relevant one being a list of libraries for doing exactly that:

https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin

The problem is, when I tried using libraries from that thread (and elsewhere) they all either:

The problem seems to be that Backbone's router watches for hashchange events, and so do these other libraries, and they do so in a way which affects Backbone's Router (eg. one made the back button completely stopp working).

So, my question is, does anyone know of a way to detect back-button presses, that works in IE8+, which (and this is the key part) doesn't break the Backbone Router?

Or failing that, can anyone even explain or point me to an explanation of how to implement back-button-prevention myself on a Backbone.Router-powered site?

Upvotes: 2

Views: 5294

Answers (2)

kehers
kehers

Reputation: 4160

If you are using pushState, you can use window.onpopstate

window.onpopstate = function(event) {
  console.log(document.location);
};

Upvotes: 1

KevJ
KevJ

Reputation: 31

If you're trying to eliminate particular clicks from being retriggered with the back button (like delete events), easiest approach is to prevent them from being added to history in the first place with preventDefault() as part of your onclick event function.

If that's your issue this would be a good article to read: http://lostechies.com/derickbailey/2011/08/03/stop-using-backbone-as-if-it-were-a-stateless-web-server/

Here's a fairly simple method for detecting a back click with Backbone: backbone routing detecting whether forward or back pressed

Upvotes: 1

Related Questions