You can use the window.history
object to navigate back to the previous page in JavaScript. The history
object provides methods to manipulate the browser’s session history, including moving backward or forward in the history stack. To go back to the previous page, you can use the back()
method:
// Go back to the previous page
function goBack() {
window.history.back();
}
// Call the function to navigate back
goBack();
Alternatively, you can also use the history.go(-1)
method to achieve the same result:
// Go back to the previous page
function goBack() {
window.history.go(-1);
}
// Call the function to navigate back
goBack();
Both of these methods will simulate the user clicking the browser’s back button and take them to the previous page in the session history.