How to go back to previous page in Javascript

You can add a Javascript button to go back to the previous page. When the user clicks on the button, they are redirected to the last page visited, as if they were clicking the back button on their browser.

You can achieve this by modifying the HTML code of your page and adding a bit of JavaScript.

Note: The button will not work if the user has no browsing history. For example, if the user opens your page in a new tab or browser window, nothing happens when they click the button.



Use history.back

In JavaScript, the history object contains the URLs that the user has visited. You can use the history.back() method to tell the browser to go back to the previous page.

Here we are creating the button, and adding the onclick event attribute. See the example below:

<button onclick="goBack()">Go Back</button>
function goBack() {
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *