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?
Asked
Active
Viewed 1.5k times
7

Peter Mortensen
- 30,738
- 21
- 105
- 131

kml_ckr
- 2,211
- 3
- 22
- 35
-
3This may well be a browser-specific limit anyway, and I'd think it'd keep the latest values and just remove the oldest at the head of the list. Are you monitoring the count specifically or the contents? – Rup May 03 '13 at 09:27
-
You could store the history in localStorage perhaps? :) – Ja͢ck May 03 '13 at 09:28
-
1What is the history needed for? – Qantas 94 Heavy May 03 '13 at 09:33
-
@Rup I need count not the content. – kml_ckr May 03 '13 at 10:57
-
@Qantas94Heavy length is used to detect back button is clicked or not – kml_ckr May 03 '13 at 10:57
-
@Rup How i will pop from window.history stack ? – kml_ckr May 06 '13 at 14:01
-
1@kml_ckr, This will not work. It could say 10 if you have "foward" entries, [even while the backward entries are 0](http://stackoverflow.com/a/9757655/632951). – Pacerier Apr 06 '15 at 07:31
2 Answers
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
-
1
-
Caveat: localStorage is shared between tabs, so opening the same page in multiple tabs will mess up this solution. – andraaspar Sep 03 '19 at 14:19
-
Instead of localStorage, keep the count in the history state using `replaceState({count}, title, href)`. That will tie it to the browser history and keep it as long as the history is kept. – andraaspar Sep 04 '19 at 12:23
-
In addition to down voting my 6 years old answer consider adding an updated answer. – Alberto Zaccagni Sep 10 '19 at 15:12
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