JavaScript History object
Last Updated :
11 Jul, 2025
The JavaScript History object contains the browser's history. First of all, the window part can be removed from window.history just using the history object alone works fine. The JS history object contains an array of URLs visited by the user. By using the history object, you can load previous, forward, or any particular page using various methods.
JavaScript history object Properties:
- length: It returns the length of the history URLs visited by the user in that session.
JavaScript history object Methods:
- forward(): It loads the next page. Provides the same effect as clicking back in the browser.
- back(): It loads the previous page. Provides the same effect as clicking forward in the browser.
- go(): It loads the given page number in the browser. history.go(distance) function provides the same effect as pressing the back or forward button in your browser and specifying the page exactly that you want to load.
Example 1: JavaScript code to show the working of history.back() function.
HTML
<strong>
Press the back button
</strong>
<input type="button"
value="Back"
onclick="previousPage()">
<script>
function previousPage() {
window.history.back();
}
</script>
Output: This example will not work if the previous page does not exist in the history list. If you click the above link then a new page opens and when you press the back button on that page it will redirect to the page that you opened previously.
Press the back button Back
Example 2: JavaScript code to show the working of history.forward() function.
HTML
<strong>
Press the forward button
</strong>
<input type="button"
value="Forward"
onclick="NextPage()">
<script>
function NextPage() {
window.history.forward()
}
</script>
Press the forward button Forward
Note: This example will not work if the next page does not exist in the history list. This code can be used when you want to use the forward button on your webpage. It works exactly the same as the forwarding button of your browser. If the next page doesn't exist it will not work.
Example 3: JavaScript code to show the working of history.go() function, go(4) has the same effect as pressing your forward button four times. A negative value will move you backward through your history in a browser.go(-4) has the same effect as pressing your back button four times.
HTML
<input type="button"
value="go"
onclick="NextPage()">
<script>
function NextPage() {
window.history.go(4);
}
</script>
Note: This example will not work if the next four pages do not exist in the history list.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Similar Reads
JavaScript - Browser Object Model The Browser Object Model (BOM) in JavaScript helps to interact with the browser, not just the webpage. While the DOM handles the content of the page, BOM gives you control over things like the browser window, the URL, and the history. This means you can do things like resize the window, go back and
4 min read
How to access history in JavaScript ? In this article, we will learn how to access history in JavaScript. We will use the History object to access the history stack in JavaScript. Every web browser will store the data on which websites or webpages opened during the session in a history stack. To access this history stack we need to use
3 min read
JavaScript Object Reference JavaScript Objects are the most important data type and form the building blocks for modern JavaScript. The "Object" class represents the JavaScript data types. Objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). It is used to st
4 min read
SVG Window.history Property The SVG Window.history property returns a reference to the History object. Syntax: var e = window.history Return value: This property returns a reference to the History object. Example 1: In this example we will use onclick event. html <!DOCTYPE html> <html> <body> <center>
1 min read
HTML DOM History go() Method The History go() method in HTML is used for loading a specific URL from the history list. It is a better alternative to history.back() and history.forward() methods if a number or the URL of the specific page is known and wants to load from your history. Syntax: history.go( number|URL )Parameters: T
2 min read
HTML DOM History length Property The History length property in HTML is used to return the count of URLs in the history list of the current browser window. The minimum value returned by this property is 1 because the current page is loaded at the moment whereas the maximum count that can be displayed is 50. Web browsers such as Int
1 min read