There are situations where giving readers a back and forward button in their navigation can improve their user experience
Thanks to the Javascript history api it is very easy to create this back and forward button and add it to your navigation.
Related: Redirect with Javascript.
Add back and forward button:
For “back” or “back” button:
<a href="javascript:history.go(-1)">Back</a>
This is the history
function and has as only parameter, this case: “-1” for a “back” link, well, to create a “forward” link just replace it with a “1”. Thus:
<a href="javascript:history.go(1)">Forward</a>
And if you needed it to be two backwards, as easy as putting a “-2” in the function.
How it works
Both buttons make use of the history.go() method that receives as a parameter the position in the history where you want to go, relative to the current page.
If history.go
receives 0
as parameter its behavior will be the same as location.reload()
, that is, refresh the current page.
i hope you find it useful!