Web History API
The Web History API offers simple ways to reach the windows.history object.
The window.history object stores the web addresses (URLs) that the user has visited.
The History back() Method
The back() function loads the URL that came before in the list of web page history.
It's similar to when you click the "back arrow" in your web browser.
Example
<button onclick="myFunction()">Go Back</button>
<script>
function myFunction() {
window.history.back();
}
</script>
The History go() Method
The go() function takes you to a particular web address from your browsing history.
Example
<button onclick="myFunction()">Go Back 2 Pages</button>
<script>
function myFunction() {
window.history.go(-2);
}
</script>
History Object Properties
Property | Description |
---|---|
length | Shows how many website links are in the history. |
History Object Methods
Method | Description |
---|---|
back() | Brings up the website you visited before this one. |
forward() | Loads the next web page you visited. |
go() | Display a particular web page previously visited. |