You can use the ‘history.back()’ method in JavaScript to navigate back to the previous page in the browser’s history. This method is a part of the ‘window.history’ object and goes back one page in the browser’s session history.
// go back to the previous page
history.back();
You could also use ‘window.history.go(-1)’ for the same result.
// go back to the previous page
window.history.go(-1);
It’s important to note that these methods will only work if the previous page was accessed via a link or the browser’s back button, and will not work if the previous page was accessed via a form submission or a browser refresh.
Another way to go back to the previous page is by using ‘window.location’ and assigning the value of ‘history.go(-1)’ to it.
window.location = history.go(-1)
You can also use ‘window.location.href = document.referrer;’ it will redirect to the previous page.
Also Read:
- What Is a Web Worker In JavaScript?
- How To Sort an Array of Strings In JavaScript?
- ECMAScript vs JavaScript
- What Is Strict Mode In JavaScript?
- Arrow Functions In JavaScript
- Difference Between Node.js and AngularJS With Example
- Difference Between == And === In JavaScript
- What Is JSON In JavaScript
- How To Detect A Mobile Device With JavaScript?
- How To Close The Current Tab In A Browser Window Using JavaScript?
- How To Convert Input Text To Uppercase While Typing Using JavaScript?
- How To Show A Confirmation Message Before Delete In JavaScript?
- How To Detect Browser or Tab Closing In JavaScript?
- How To Get Hash Value From URL Using JavaScript?
- How To Get The Name, Size, And Type Of A File In JavaScript?
- Run JavaScript From The Command Line