The Web History API offers convenient methods to interact with the window.history
object, which stores the URLs (websites) visited by the user.
The Web History API is compatible with all major browsers.
The back()
method loads the previous URL from the window.history
list, similar to clicking the “back arrow” in your browser.
<button onclick=”myFunction()”>Go Back</button> <script> function myFunction() { window.history.back(); } </script> |
The go()
method loads a specific URL from the history list based on a given index.
<button onclick=”myFunction()”>Go Back 2 Pages</button> <script> function myFunction() { window.history.go(-2); } </script> |
Property |
Description |
Length |
Returns the total number of URLs in the history list. |
Method |
Description |
back() |
Loads the previous URL from the history list. |
forward() |
Loads the next URL in the history list. |
go() |
Loads a specific URL from the history list based on the given index. |