The History object is referenced via the Window objects history property. It has a list of all the pages visited in the current frame or window, as well as the browser session history.
Method | Description | Example |
---|---|---|
forward() | Next page is loaded | history.forward() |
back() | Previous page is loaded | history.back() |
go() | Specific page number is loaded | history.go(5) |
<!DOCTYPE html>
<html lang="en">
<body>
<script>
function show() {
alert(history.length + " current browser session");
}
</script>
<button type="button" onclick="show();">Show</button>
</body>
</html>
<script>
function nextPage() {
window.history.forward();
}
</script>
<button type="button" onclick="nextPage();">Next</button>
<script>
function previousPage() {
window.history.back();
}
</script>
<button type="button" onclick="previousPage();">Back</button>
window.history.go(2); // Go forward two pages
window.history.go(4); // Go forward four pages
window.history.go(-2); // Go back two pages
window.history.go(-4); // Go back four pages
window.history.go(0); // Reload the current page