7

The value of window.history.length is very important in our project to detect the back button is clicked in the browser. However, I realized that window.history.length does not pass 50. How can I solve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kml_ckr
  • 2,211
  • 3
  • 22
  • 35

2 Answers2

5

Depending on whether you need it to be persistent across sessions and surviving a clean of the user information (cache, localStorage, etc.), you might want to adopt different solutions.

One of the solutions could be to do something like this:

window.onpopstate = function(event) {
  var count = parseInt(localStorage.getItem('history-changes-count'), 10);
  localStorage.setItem('history-changes-count', ++count);
};

Note that onpopstate gets invoked only after a user action. It doesn't work if you modify the history programmatically.

More on the subject: Window: popstate event

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
1

It is possible to detect "Back Button Clicked" via iFrames. You can find the answer at Browser Back Button Detection.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sedat Polat
  • 1,631
  • 2
  • 18
  • 28