Andrew Bullock
Andrew Bullock

Reputation: 37378

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

Upvotes: 3

Views: 166

Answers (1)

timrs2998
timrs2998

Reputation: 1911

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.

Upvotes: 2

Related Questions