Javascript history

Going back

Javascript has two methods of going backwards in the user history.

history.back();
history.go(-1);

These examples take the user back one page, but this can be easily changed to make the user go back many pages.

history.go(-2);

Forward

In the same way as sending a user back a page, you can redirect them forward in their browsing history.

history.forward();
history.go(1);

Again if the user has many forward pages you can redirect them to any of them.

history.go(2);

A simple back button

Using the code above a simple back button can be created.

<input name="back" type="button"value="Back" onclick="javascript:history.go(-1)" />
<input name="forward" type="button" value="Forward" onclick="javascript:history.go(1)" />

Categories

Tags

No tags

Social