The JavaScript history object represents an array of URLs visited by the user. By using this object, you can navigate to previous, forward, or any particular page in the session history.
The history object is a property of the window object, so it can be accessed using window.history or simply history.
| No. | Property | Description |
|---|---|---|
| 1 | length | Returns the number of URLs in the history list. |
| No. | Method | Description |
|---|---|---|
| 1 | forward() | Loads the next page in the session history list. |
| 2 | back() | Loads the previous page in the session history list. |
| 3 | go() | Loads a specific page in the session history list based on its relative position to the current page. |
<button onclick="goBack()">Go Back</button>
<button onclick="goForward()">Go Forward</button>
<button onclick="goToPage(2)">Go Forward 2 Pages</button>
<button onclick="goToPage(-2)">Go Back 2 Pages</button>
<p>History Length: <span id="historyLength"></span></p>
<script>
function goBack() {
history.back();
}
function goForward() {
history.forward();
}
function goToPage(pageNumber) {
history.go(pageNumber);
}
document.getElementById("historyLength").textContent = history.length;
</script>