how to go back in javascript

To go back to the previous page in the browser's history using JavaScript, you can use the history.back() method or window.history.back() method.

Here's an example of using history.back() method:

index.tsx
<button onclick="goBack()">Go Back</button>

<script>
function goBack() {
  window.history.back();
}
</script>
111 chars
8 lines

This will add a button to the page that, when clicked, will go back to the previous page in the browser's history.

Alternatively, you can simply use the window.history.back() method without a button:

index.tsx
<script>
window.history.back();
</script>
42 chars
4 lines

This will immediately go back to the previous page in the browser's history.

gistlibby LogSnag