The History
property in JavaScript is a property of the Window
object that provides methods and properties for accessing and manipulating the browser’s history. The History
object allows you to navigate the user’s browsing history, and add, replace, or remove entries in the history.
Here is an example of how the History
property works:
function goBack() {
window.history.back();
}
function goForward() {
window.history.forward();
}
function goTo(url) {
window.history.pushState({}, '', url);
}
function replaceCurrent(url) {
window.history.replaceState({}, '', url);
}
In the code example above, we define four functions that use the History
property to manipulate the browser’s history. The goBack()
function uses the history.back()
method to navigate the user to the previous page in the history. The goForward()
function uses the history.forward()
method to navigate the user to the next page in the history.
The goTo()
function uses the history.pushState()
method to add a new entry to the history and navigate the user to the specified URL. The replaceCurrent()
function uses the history.replaceState()
method to replace the current entry in the history with the specified URL.
The History
object provides a number of useful methods and properties for accessing and manipulating the browser’s history. Some of the most commonly used methods and properties include:
history.back()
: navigates the user to the previous page in the historyhistory.forward()
: navigates the user to the next page in the historyhistory.go()
: navigates the user to a specific page in the historyhistory.pushState()
: adds a new entry to the history and navigates the user to the specified URLhistory.replaceState()
: replaces the current entry in the history with the specified URLhistory.length
: returns the number of entries in the history
By using the History
property and its various methods and properties, developers can create web pages that allow the user to navigate through the history and interact with the browser’s history in a more intuitive and user-friendly way.
Leave a Reply