Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if there is a `Back` state available without navigating?

Is it possible to tell if there is a back state available (i.e. if the back button in the browser is clickable) without navigating?

I'd like to be able to do this in HTML4 and HTML5 if possible.

I can see there's window.history.length, but it doesn't seem to have coherent or consistent behaviour.

Is this possible?

I can see there is this thread, but it's rather old. Was wondering if there were HTML5 changes since this thread: How to check if the user can go back in browser history or not

like image 239
Andrew Bullock Avatar asked Jan 26 '26 21:01

Andrew Bullock


1 Answers

From MDN, it seems that when window.history.length is 1, the current page is the only page in history. Anything greater than 1 should mean that the user can navigate backwards. However this is only true for Firefox and Chrome. Looking at w3schools it states

Note: Internet Explorer and Opera start at 0, while Firefox, Chrome, and Safari start at 1.

So then anything greater than 0 in IE and Opera means that the user can navigate backwards.

like image 103
timrs2998 Avatar answered Jan 28 '26 11:01

timrs2998



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!