Ryan Ryan - 4 months ago 21
Javascript Question

How to check if the user can go back in browser history or not

I want using JavaScript to see if there is history or not, I mean if the back button is available on the browser or not.

Answer

The only accurate way would be checking the property:

history.previous

However, the problem with this is that in most browsers this is considered a security violation and usually just returns undefined.

history.length

Is a property that others have suggested...
However, the length doesn't work completely because it doesn't indicate where in the history you are. Additionally, it doesn't always start at the same number. IE, for example, starts at 0 while Google Chrome starts at 1 (because of the landing page).

alt text

Most of the time a link is added that calls:

history.back();

or

 history.go(-1);

and it's just expected that if you can't go back then clicking the link does nothing.