DEV Community

Cover image for Do you know history.back() in JavaScript?
Dhairya Shah
Dhairya Shah

Posted on β€’ Originally published at codewithsnowbit.hashnode.dev

3 2

Do you know history.back() in JavaScript?

So, we all surf the internet to watch YouTube videos and Stackoverflow and many more. And we usually click "←" this button to go back to the previous website.

back button

Do you know?
You can create the same functionality of "←" this button of browser in Javascript πŸ”₯


Let's create a back button

<button>Go Back</button>
Enter fullscreen mode Exit fullscreen mode

It's time to make it functional ✌️

  • Add onclick attribute to the button
onclick="history.back()"
Enter fullscreen mode Exit fullscreen mode
  • Here's the final and little code πŸ™‚
<button onclick="history.back()">Go Back</button>
Enter fullscreen mode Exit fullscreen mode

How does this work?

The history.back() method loads the previous URL from the history list. It will only work if the previous page exists.

Try it live by yourself: Live demo (Open this link in this tab only since it will create a previous page πŸ™‚ )


Feel free to reach me out via Twitter - @codewithsnowbit

🌏 Let's connect

Stay tuned for the next article. Stay safe and Happy Coding!

If you enjoyed and find my content helpful, then you can check this out
Buy me a coffee

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay