History Object
In this tutorial, we will learn about the History object in JavaScript.
In JavaScript, the history object represents the browsing history of the current window. It allows you to navigate back and forward through the history stack, as well as to move to a specific page in the history. The history object is automatically created by the browser and is available to all JavaScript code running in the browser.
Accessing the History Object
You can access the history object in JavaScript using the window.history property. For example, you can use the history object to navigate back and forward through the browser's history stack:
// Go back one page in the history stack
window.history.back();
// Go forward one page in the history stack
window.history.forward();
// Go to a specific page in the history stack
window.history.go(-2); // Go back two pagesThese methods allow you to navigate through the browser's history stack and move to specific pages in the history.
Properties of the History Object
The history object has several properties that provide information about the browser's history stack. Some of the commonly used properties are:
history.length: Returns the number of pages in the history stack.history.state: Returns the state object associated with the current history entry.
You can access these properties using the history object. For example, to get the number of pages in the history stack, you can use window.history.length.
Methods of the History Object
The history object also has several methods that allow you to interact with the browser's history stack. Some of the commonly used methods are:
history.back(): Moves back one page in the history stack.history.forward(): Moves forward one page in the history stack.history.go(): Moves to a specific page in the history stack.history.pushState(): Adds a new entry to the history stack.
You can call these methods using the history object. For example, to move back one page in the history stack, you can use window.history.back().
Using pushState() Method
The pushState() method allows you to add a new entry to the history stack without navigating to a new page. This method takes three arguments: a state object, a title, and a URL. Here is an example:
// Add a new entry to the history stack
window.history.pushState({ page: 1 }, "Page 1", "/page1");This will add a new entry to the history stack with the state object { page: 1 }, the title "Page 1", and the URL "/page1". The page will not be navigated to the new URL, but the new entry will be added to the history stack.
Conclusion
- The
historyobject represents the browsing history of the current window in JavaScript. - You can access the
historyobject using thewindow.historyproperty. - The
historyobject has properties and methods that allow you to navigate back and forward through the browser's history stack. - The
pushState()method allows you to add new entries to the history stack without navigating to new pages.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on