November 8, 2023
window object's history API. This guide provides methods to retrieve the URL that the user visited before landing on the current page.
Understanding the History API
The History API provides various properties and methods to interact with the browser history. The
window.history object is key to navigating this history stack.
Retrieve the Previous URL
To get the previous URL, you can use the
document.referrer property, which returns the URI of the page that linked to the current page.
document.referrer has limitations. It won't work for all scenarios, particularly if the user navigated directly to a page or if the previous site uses the
Using the History API
For more control over the navigation history, you can use the History API methods, like
history.back(), which navigates to the previous page in the history stack.
Capturing URL During Navigation
To capture URLs as the user navigates, consider setting up event listeners that track history state changes.
When changing the history state, you can store the previous URL.
document.referrer and the History API offer partial solutions, while custom state management can provide more robust tracking of navigation history.
How to Truncate Date in MySQL